0% found this document useful (0 votes)
12 views119 pages

Chapter - 3

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views119 pages

Chapter - 3

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 119

Chapter three

Computer Organization
Outline
 Memory
 CPU
 Computer organization
 The 80x86 family of CPUs
 Interrupts
 Registers
Interrupts
 Outline
 Interrupts
 Purpose of Interrupts
 Type of Interrupts
 Interrupt vectors
 Interrupt vector table
Interrupt
 An Interrupt is either a Hardware
generated CALL (externally
derived from a hardware signal)
OR
 A Software-generated CALL
(internally derived from the
execution of an instruction or by
some other internal event
 An interrupt is used to cause a temporary halt in the
execution of program.
 The meaning of ‘interrupts’ is to break the sequence
of operation.
 While the Microprocessor is executing a program, an
‘interrupt’ breaks the normal sequence of execution
of instructions, diverts its execution to some other
program called Interrupt Service Routine (ISR).
 After executing ISR, IRET returns the control back
again to the main program. Interrupt processing is an
alternative to polling.
Interrupts
 The keyboard controller can hold only a single keystroke. Therefore, the keyboard
controller must be freed before the next keystroke arrives.
 The keystroke is passed to the CPU by putting it in the keyboard buffer.
 So, the keyboard controller keeps on passing the keystroke input to the CPU,
 but how does the CPU attend to it? The CPU is not at the disposal of the keyboard
controller; it is usually busy doing several other operations.
 So, we need some mechanism to indicate to the CPU that a keystroke has arrived.
How is this done? There are two approaches to making sure that the CPU pays
attention:
 The CPU executes other program, as soon as a key is
pressed, the Keyboard generates an interrupt.
 The CPU will response to the interrupt – read the data.
After that returns to the original program.
 So by proper use of interrupt, the CPU can serve
many devices at the “same time”
The Purpose of Interrupts
 Interrupts are useful when interfacing I/O devices at
relatively low data transfer rates, such as keyboard inputs.
 Interrupt processing allows the processor to execute other
software while the keyboard operator is thinking about what
to type next. When a key is pressed, the keyboard encoder
debounces the switch and puts out one pulse that interrupts
the microprocessor.
• a time line shows typing on a keyboard, a printer
removing data from memory, and a program
executing
• the keyboard interrupt service procedure, called
by the keyboard interrupt, and the printer
interrupt service procedure each take little time to
execute
Types of interrupts
Types Of Interrupt
 SOFTWARE INTERRUPTS: There are instructions in 8086 which
cause an interrupt.
 INT instructions with type number specified.
 INT 3, Break Point Interrupt instruction.
 INTO, Interrupt on overflow instruction.
 HARDWARE INTERRUPTS: The primary sources of interrupts,
however, are the PCs timer chip, keyboard, serial ports, parallel
ports, disk drives, CMOS real time clock, mouse, sound cards, and
other peripheral devices.
Interrupt Vectors
 The interrupt vector table contains 256 four byte entries, containing
the CS:IP
 Interrupt vectors for each of the 256 possible interrupts.
 The table is used to locate the interrupt service routine addresses for
each of those interrupts. The Interrupt vector table is located in the
first 1024 bytes of memory at addresses 000000H-0003FFH.It
contains the address(segment and offset)of the interrupt service
provider
The interrupt vector table for the microprocessor and (b) the contents of an interrupt vector.

 the first five interrupt vectors are identical


in all Intel processors – Intel reserves the
first 32 interrupt vectors
 the last 224 vectors are user available
 each is four bytes long in real mode and
contains the starting address of the
interrupt service procedure.
 the first two bytes contain the offset
address
 the last two contain the segment address
What happen the process In the processor?
e.g.
Summary
Some special types of interrupts in the
intel series
Type 2
 The non-maskable interrupt
occurs when a logic 1 is
placed on the NMI input pin
to the microprocessor.
Nonmaskable
 it cannot be disabled
Cont…
 Type3
 A special one-byte instruction (INT 3) that uses this vector
to access its interrupt-service procedure.
 often used to store a breakpoint in a program for debugging
 Type 4
 Overflow is a special vector
used with the INTO
instruction.
 The INTO instruction
interrupts the program if an
overflow condition exists.
Type 5
 The BOUND instruction compares a register with
boundaries stored in the memory. If the contents of the
register are greater than or equal to the first word in
memory and less than or equal to the second word, no
interrupt occurs because the contents of the register are
within bounds. if the contents of the register are out of
bounds, a type 5 interrupt ensues as reflected by the
overflow flag (OF)
Cont…
Type 7
 The coprocessor not available
interrupt occurs when a coprocessor
is not found, as dictated by the
machine status word (MSW or CR0)
coprocessor control bits.
 if an ESC or WAIT instruction
executes and no coprocessor is found,
a type 7 exception or interrupt occurs
 A double fault
interrupt is activated
when two separate
interrupts occur
during the same
instruction.
 In computing, a double fault is a serious type
of error that occurs when a central processing
unit (CPU) cannot adequately handle a
certain type of system event that requires the
CPU’s immediate attention.
 Double faults may cause computer crashes
and error messages, automatic restarting of
the machine, and the loss of any unsaved
data. They are often caused by problems in
the computer’s hardware such as a bad
memory module or overheating CPU.
Type 9
 The coprocessor segment overrun
occurs if the ESC instruction
(coprocessor opcode) memory operand
extends beyond offset address
FFFFFH in real mode.
Cont..
 Type 10
 An invalid task state segment interrupt occurs in the
protected mode if the TSS is invalid because the segment
limit field is not 002BH or higher. usually because the TSS
is not initialized
 Type 11
 The segment not present interrupt occurs when the
protected mode P bit (P = 0) in a descriptor indicates that
the segment is not present or not valid.
 Type 12  Type 13
 A stack segment overrun  The general protection fault

occurs if the stack segment occurs for most protection


is not present (P = 0) in the violations in 80286–Core2 in
protected mode or if the protected mode system. These
limit of the stack segment is errors occur in Windows as
exceeded. general protection faults.
 A list of these protection

violations follows
Cont…
 PROTECTION VIOLATIONS  Type 14
 (a) Descriptor table limit exceeded

 (b) Privilege rules violated


 Page fault interrupts occur for
 (c) Invalid descriptor segment type loaded
any page fault memory or
 (d) Write to code segment that is protected
code access in 80386, 80486,
 (e) Read from execute-only code segment
and Pentium–Core2
 (f) Write to read-only data segment processors.
 (g) Segment limit exceeded o (h) CPL = IOPL

when executing CTS, HLT, LGDT, LIDT, LLDT,


LMSW, or LTR
 (i) CPL > IOPL when executing CLI, IN, INS,

LOCK, OUT, OUTS, and STI


Cont…
 Type 16  Type 17
 Coprocessor error takes effect  Alignment checks indicate
when a coprocessor error word and double word data
(ERROR = 0) occurs for ESCape are addressed at an odd
or WAIT instructions for 80386, memory location (or
80486, and Pentium–Core2 only. incorrect location, in the
case of a double word).
 interrupt is active in 80486
and Pentium–Core2
 Type 18
 A machine check activates a system memory management
mode interrupt in Pentium– Core2.
Assembly language fundamentals

Chapter four
Outline
 Introduction to Assembly Language
 Basic Elements of Assembly Language
 Assembly, Machine, and High-Level Languages
 Defining Data Types
 Assembly Directives
 Assembly Language Programming Tools
Introduction
Levels of Programming Languages
1) Machine Language
 Consists of individual instructions that will be executed by the CPU one at a time
2) Assembly Language (Low Level Language)
 Designed for a specific family of processors (different processor groups/family has different
Assembly Language)
 Consists of symbolic instructions directly related to machine language instructions one-for-one
and are assembled into machine language.
3) High Level Languages
 e.g. : C, C++ and Vbasic

 Designed to eliminate the technicalities of a particular computer.


 Statements compiled in a high level language typically generate many low-level instructions.

 HLL programs are machine independent.


 They are easy to learn, easy to use, and convenient for managing complex tasks.
Advantages of Assembly Language
1. Shows how program interfaces with the processor,
operating system, and BIOS(basic input/output system).
2. Shows how data is represented and stored in memory and
on external devices.
3. Clarifies how processor accesses and executes instructions
and how instructions access and process data.
4. Clarifies how a program accesses external devices.
Reasons for using Assembly Language
1. A program written in Assembly Language requires considerably less memory and
execution time than one written in a high –level language.
2. Assembly Language is useful for implementing system software and also useful
for small embedded system applications
3. Assembly Language gives a programmer the ability to perform highly
technical tasks that would be difficult, if not impossible in a high-level
language.
4. Although most software specialists develop new applications in high-level
languages, which are easier to write and maintain, a common practice is to recode
in assembly language those sections that are time-critical.
5. Resident programs (that reside in memory while other program
execute) and interrupt service routines (that handle input and output)
are almost always develop in Assembly Language.
Assembly vs HLL
Basic Elements of Assembly Language
 Integer constants
 Integer expressions
 Character and string constants
 Reserved words and identifiers
 Directives and instructions
 Labels
 Mnemonics and Operands
 Comments
 Examples

45
Integer Constants
 Optional leading + or – sign
 binary, decimal, hexadecimal, or octal digits
 Common radix characters:
 h – hexadecimal
 d – decimal
 b – binary
 r – encoded real
Examples: 30d, 6Ah, 42, 1101b
Hexadecimal beginning with
46 letter: 0A5h
Integer Expressions
 Operators and precedence levels:

 Examples:
47
Character and String Constants
 Enclose character in single or double quotes
 'A', "x"
 ASCII character = 1 byte
 Enclose strings in single or double quotes
 "ABC"
 'xyz'
 Each character occupies a single byte
 Embedded quotes:
 'Say "Goodnight," Gracie'
48
Reserved Words and Identifiers
 Reserved words cannot be used as identifiers
 Instruction mnemonics(such as MOV, ADD, and MUL),
directives, type attributes, operators, predefined symbols
 Identifiers
 1-247 characters, including digits

 case insensitive (by default)

 first character must be a letter, _, @, or $

 Examples: var1, Count, $first, _main, MAX ,


open_file, xVal

49
Directives
 Commands that are recognized and acted upon by the
assembler
 Not part of the Intel instruction set
 Used to declare code, data areas, select memory model,
declare procedures, etc.
 E.g. myVar DWORD 26 ; DWORD directive
 move ax, myVar ; MOV instruction
 Different assemblers have different directives
 NASM != MASM, for example

50
Directives
 In MASM, directives are case insensitive.
 different types directives
 Defining Segments: One important function of assembler directives is to define
program section, or segments.
 .DATA directive identifies the area of a program containing variables:
 .data

 .CODE directive identifies the area of a program containing


instructions:
 .code
 .STACK directive identifies the area of a program holding the runtime stack, setting its
size:
 .stack 1000h
Directives
 Proc:
 End :
 Directive identifies the beginning of a  Directive marks the last line of the program to be
procedure assembled. It identifies the name of the program’s
startup procedure (the procedure that starts the
 Endp: program execution.) Procedure main is the
startup procedure.
 Directive marks the end of the  Title:
procedure  Directive marks the entire line as a comment

 .model
 directive instructs the assembler to generate
code for a protected mode program, and
STDCALL enables the calling of MS-
Windows functions.
 Flat, small
Instructions
 An instruction is a statement that becomes executable  We use the Intel IA-32 instruction set
when a program is assembled.
Syntax:
 Instructions are translated by the assembler into
machine language bytes, which are loaded and [label] mnemonic(opcode) operand(s) [;comment]
executed by the CPU at run time. label optional
 The major two fields are: instruction mnemonic required: such as MOV, ADD,
SUB, MUL
 Opcode field which stands for operation code
operands usually required
and it specifies the particular operation that is to
comment optional
be performed.
 An instruction contains:
 Each operation has its unique opcode.
 Operands fields which specify where to get the  Label
source and destination operands for the  Mnemonic
operation specified by the opcode.  Operand
 The source/destination of operands can be a constant,  Comment
the memory or one of the general-purpose registers. 53
Labels
 Act as place markers  Code label
 marks the address (offset) of  target of jump and loop
code and data instructions
 Follow identifier rules  example: L1:
 MOV ax, bx …
 Data label  JMP L1
 must be unique  (followed by colon)
 example:
 count DWORD 100
 (not followed by colon)

54
Mnemonics and Operands
 Instruction Mnemonics
 "reminder"
 examples: MOV, ADD, SUB, MUL, INC, DEC
 Operands
 constant (immediate value i.e. 4 or 0-9)
 constant expression(2*4)
 Register(eax, ax)
 memory (data label)
55
Comments
 Comments are good!  Multi-line comments
 explain the program's purpose  begin with COMMENT directive and
a programmer-chosen character
 when it was written, and by  end with the same programmer-
whom chosen character
 revision information  Example:
 tricky coding techniques
 application-specific  COMMENT ! This is a comment.
explanations This line is also a comment. !
 Single-line comments
 begin with semicolon (;)
Instruction Format Examples
 No operands
 stc ; set Carry flag
 One operand
 inc eax ; register
 inc myByte ; memory
 Two operands
 add ebx,ecx ; register, register
 sub myByte,25 ; memory, constant
 add eax,36 * 25 ; register, constant-expression
Suggested Coding Standards
 Some approaches to capitalization
 capitalize nothing
 capitalize everything
 capitalize all reserved words, including instruction mnemonics and
register names
 capitalize only directives and operators
 Other suggestions
 descriptive identifier names
 spaces surrounding arithmetic operators
 blank lines between procedures
Suggested Coding Standards
 Indentation and spacing
 code and data labels – no indentation
 executable instructions – indent 4-5 spaces
 comments: begin at column 40-45, aligned vertically
 1-3 spaces between instruction and its operands
 ex: mov ax,bx
 1-2 blank lines between procedures
Program Template
 TITLE Program Template (Template.asm)
 ; Program Description:
 ; Author:
 ; Creation Date:
 ; Revisions:
 ; Date: Modified by:

 INCLUDE Irvine32.inc
 .data; (insert variables here)
 .code
 main PROC ; (insert executable instructions here)
 exit ;exit to operating system
 main ENDP; (insert additional procedures here)
 END main
Example: Adding and Subtracting Integers
 TITLE Add and Subtract (AddSub.asm)
 ; This program adds and subtracts 32-bit integers.
 INCLUDE Irvine32.inc
 .code
 main PROC
 mov eax,10000h ; EAX = 10000h
 add eax,40000h ; EAX = 50000h
 sub eax,20000h ; EAX = 30000h
 call DumpRegs ; display registers//EAX=00030000
 exit
 main ENDP
 END main
Assembly Language Programming Tools
 Software tools are needed for editing, assembling, linking, and
debugging assembly language programs
 An assembler is a program that converts source-code
programs written in assembly language into object files in
machine language
 Popular assemblers includes …
 TASM (Turbo Assembler from Borland)
 NASM (Netwide Assembler for both Windows and Linux), and
 GNU assembler distributed by the free software foundation
 MASM- Microsoft Macro Assembler
Linker and Link Libraries
 You need a linker program to produce executable files

 It combines your program's object file created by the assembler with


other object files and link libraries, and produces a single executable
program

 LINK32.EXE is the linker program provided with the MASM distribution


for linking 32-bit programs

 We will also use a link library for input and output

 Called Irvine32.lib developed by Kip Irvine


 Works in Win32 console mode under MS-Windows
Assemble and Link Process
Source Object
File Assembler File

Source Object Executable


File Assembler File Linker
File

Source Object Link


File Assembler File Libraries

 A project may consist of multiple source files


 Assembler translates each source file separately into an object file
 Linker links all object files together with link libraries
Debugger
 Allows you to trace the execution of a program
 Allows you to view code, memory, registers, etc.
 Example: 32-bit Windows debugger
Editor
 Allows you to create assembly language source files
 Some editors provide syntax highlighting features and
can be customized as a programming environment
 Notepad, visual studio 2010 C++ express
Defining Data
 Intrinsic Data Types
 Data Definition Statement
 Defining BYTE and SBYTE Data
 Defining WORD and SWORD Data
 Defining DWORD and SDWORD Data
 Defining QWORD Data
 Defining TBYTE Data
 Defining Real Number Data
 Little Endian Order
 Declaring Uninitialized Data
Intrinsic Data Types
 Intrinsic Data Types  QWORD
 BYTE, SBYTE ƒ8-bit unsigned  64-bit integer
integer;  TBYTE
 8-bit signed integer  80-bit integer

 WORD, SWORD  REAL4


 4-byte IEEE short real
 16-bit unsigned & signed integer
 DWORD, SDWORD  REAL8
 8-byte IEEE long real
 32-bit unsigned & signed integer
 REAL10
 10-byte IEEE extended real
Data Definition Statement
 A data definition statement sets  Defining BYTE and SBYTE
aside storage in memory for a Data
variable. o May optionally assign a
name (label) to the data o Syntax:
 [name] directive initializer [,initializer]
...
 Example: value1 BYTE 10  Defining Byte Arrays
 All initializers become binary data in • Examples: use multiple initializers
memory list1 BYTE 10, 20, 30, 40
 Defining Strings  Examples:
 A string is implemented as an array  str1 BYTE "Enter your name", 0
of characters  str2 BYTE 'Error: halting program',
 For convenience, it is usually 0
enclosed in quotation marks  str3 BYTE 'A','E','I','O','U'
 It often will be null-terminated  greeting BYTE "Welcome to the
(containing 0). Strings of this type are Encryption program " BYTE
used in C, C++, and Java programs. "created by Kip Irvine.", 0
 Using the DUP Operator
 Use DUP to allocate (create space for) an array or string.
 Syntax:
 counter DUP ( argument )
 Counter and argument must be constants or constant
expressions
 Examples:
 Defining WORD and SWORD Data • Defining DWORD and SDWORD Data
 Defining WORD and SWORD Data o  Defining DWORD and SDWORD Data
Define storage for 16-bit integers, single  Storage definitions for signed and unsigned
value or multiple valus 32-bit integers

 Defining QWORD, TBYTE, Real Number Data


 Defining QWORD, TBYTE, Real Data
 Storage definitions for quadwords, tenbyte
values, and real numbers
Little Endian Order
 Little Endian Order:  Big Endian Order
 All data types larger than a byte store
their individual bytes in reverse order  val1 DWORD 12345678h
 The least significant byte occurs at the
first (lowest) memory address
 Example:
 val1 DWORD 12345678h


Symbolic Constants
 Associate and identifier (a  Equal-Sign Directive
Syntax: name = expression
symbol) with an integer 
 expression is a 32-bit integer (expression or
expression or some text constant)
 may be redefined
 Symbols do not reserve  name is called a symbolic constant

storage  good programming style to use


 Used only by the assembler symbols
 COUNT = 500 . .
when scanning a program  mov al, COUNT

 Cannot change at run time 


EQU Directive TEXTEQU Directive
 Define a symbol as either an integer or text
expression.  Define a symbol as either an integer or
 Cannot be redefined text expression.
 Syntax  Called a text macro
 name EQU expression ; integer expression  Can be redefined
 name EQU symbol ; existing symbol name
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?">
 name EQU <text> ; any text
rowSize = 5
 Example .data
matrix EQU 10 * 10 prompt1 BYTE continueMsg
PI EQU <3.1416> count TEXTEQU %(rowSize * 2) ; evaluates the expression
pressKey EQU <"Press any key to continue...",0> setupAL TEXTEQU <mov al,count>
.data .code
prompt BYTE pressKey setupAL ; generates: "mov al,10"
MI WORD matrix
Outline( lab)
 Tools and setups
 Notepad, notepad++, any other
 Assembler (MASM)
 Linker
 Assembling linking
 Step in execution
 Registers and memory
Tools
 DOSBox: download DOSBox 0.74 and install.
 Notepad: we can use notepad editor, in addition there are
another editor like visual studio c++ express and other.
 Assembler (MASM): its task is to assemble the programs written
in assembly language. It generate an object file for separate
files.
 Alternatively you can download 8086 MASM assembler which
contains all the tools in it.
 Linker: link the object file with the link library
 Debug: it helps for different purpose
How to run?
 First write the code in notepad, for simplicity.
 Open your DOSBox
 To mount to the directory where your file is found write in the
command as
 Mount c c:\ or directly to your folder as, mount c c:/foldername
 Then, type c:
 Then it would be in your directory.
 Then to assemble, type masm filename.asm, enter until c:\> is
seen.
 Then to link, type link filename
 Finally use debug or afdebug command to execute the
program. C:\>debug filename.exe
 -t ; for single step execution
 -g ; for at a time execution
 -I ; for restarting the program execution
 -d ; to see the data segment
 -q ; to quit the execution
 -g ; complete execution of program in single step.
 -t ; Stepwise execution.
 -d ds: starting address or ending address ; To see data in memory locations
 -p ; Used to execute interrupt or procedure during stepwise execution of
program
 Or just type debug.exe and enter then press ? And enter.
 Or just type , debug.exe
 Then type, ? Then the following command will be
shown.
Example
 You can write codes in the debug.exe command
 E.g. addition of two numbers
 Push, pop and xchg
Decrement, subtraction and increment
 First once you mount c:\>8086> debug.exe
Other way
 E.g displaying the text “hello world”
Addition of two numbers
Interrupt instructions
 MS-DOS uses INT 21H for its main API functions which provide a
low-level interface to the devices-reading input from
keyboard, writing to terminal, create/read/write files and directories
etc. MS-DOS uses other interrupts to provide other services.
 INT is an assembly language instruction for x86 processors that
generates a software interrupt. ... For example, INT 21H will generate
the software interrupt0x21 (33 in decimal), causing the function
pointed to by the 34th vector in theinterrupt table to be executed,
which is typically an MS-DOS API call.
 INT 03H: Breakpoint Interrupt. The INT 03H vector is used by
debugging utilities in order to intercept execution when it reaches a
user-selected address. The opcode for INT 03H is one byte (c0H),
so it can lay over top of the start of any CPU instruction, without any
chance of overwriting the code that follows it.
 mov ah,4ch is the first line of assembler code. The value 4C in
hexadecimal is stored in the register AH. int 21h is the second line
of assembler code. The software interrupt 21h is called. This
interrupt, when given the value of 4ch in AH (as is the case here),
causes the program to exit immediately.
Basic Assembly Language

Chapter Five
Outline
 Branch instruction
 Conditional loop instruction
 Comparisons
 Conditional control Instructions
 Decision Directive
JMP and LOOP Instruction
 A transfer of control, or branch, is a way of altering the order in which
statements are executed. There are two basic types of transfers:
 Unconditional Transfer: Control is transferred to a new location in all cases; a new address
is loaded into the instruction pointer, causing execution to continue at the new address. The
JMP instruction does this.
 Conditional Transfer: The program branches if a certain condition is true. A wide variety
of conditional transfer instructions can be combined to create conditional logic structures.
The CPU interprets true/false conditions based on the contents of the ECX and Flags
registers.
Unconditional Transfer
 CALL : Unconditional Call
 This instruction is used to call a Subroutine (Procedure) from
a main program. Address of procedure may be specified
directly or indirectly.
 There are two types of procedure depending upon whether it
is available in the same segment or in another segment.
 i. Near CALL i.e., ±32K displacement.
 ii. For CALL i.e., anywhere outside the segment.
Cont…
 On execution this instruction stores the incremented IP & CS onto
the stack and loads the CS & IP registers with segment and offset
addresses of the procedure to be called.
 RET: Return from the Procedure.
 At the end of the procedure, the RET instruction must be executed. When it
is executed, the previously stored content of IP and CS along with Flags are
retrieved into the CS, IP and Flag registers from the stack and execution of
the main program continues further.
Cont…
 INT N: Interrupt Type N.
 In the interrupt structure of 8086, 256 interrupts are defined corresponding to
the types from 00H to FFH. When INT N instruction is executed, the type
byte N is multiplied by 4 and the contents of IP and CS of the interrupt
service routine will be taken from memory block in 0000 segment.
 INTO: Interrupt on Overflow
 This instruction is executed, when the overflow flag OF is set. This is
equivalent to a Type 4 Interrupt instruction.
JMP: Unconditional Jump
 JMP Instruction The JMP instruction causes an unconditional transfer to a
destination, identified by a code label that is translated by the assembler into an
offset. The syntax is
JMP destination

 When the CPU executes an unconditional transfer, the offset of destination is


moved into the instruction pointer, causing execution to continue at the new
location. Creating a Loop The JMP instruction provides an easy way to create a
loop by jumping to a label at the top of the loop:
top: . .
jmp top ;repeat the endless loop
JMP is unconditional, so a loop like this will continue endlessly unless another way is found to
exit the loop.
LOOP Instruction
 The LOOP instruction, formally known as Loop According to ECX Counter, repeats a block of
statements a specific number of times. ECX is automatically used as a counter and is
decremented each time the loop repeats. Its syntax is
LOOP destination

 The loop destination must be within -128 to +127 bytes of the current location counter. The
execution of the LOOP instruction involves two steps:
 First, it subtracts 1 from ECX.
 Next, it compares ECX to zero.
 If ECX is not equal to zero, a jump is taken to the label identified by destination.
 Otherwise, if ECX equals zero, no jump takes place, and control passes to the instruction following the loop.
 In the following example, we add 1 to AX each time the loop repeats. When the loop ends, AX
= 5 and ECX = 0:
 Nested Loops When creating a loop inside another loop, special
consideration must be given to the outer loop counter in ECX. You can
save it in a variable:

As a general rule, nested loops more


than two levels deep are difficult to
write. If the algorithm you’re using
requires deep loop nesting, move some
of the inner loops into subroutines.
Cont…
Here’s a short program that sums an array of 16-bit
integers.
LOOPNZ and LOOPNE
 LOOPNZ (LOOPNE) is a conditional loop instruction •Syntax:
LOOPNZ destination
LOOPNE destination

 Logic:
 ECX ←ECX –1;
 if ECX > 0 and ZF=0, jump to destination
 Useful when scanning an array for the first element that matches a given value.
LOOPNZ
The following code finds the first positive value in an array:
Conditional jumps
 Conditional Structures
 There are no explicit high-level logic structures in the x86 instruction set, but you can
implement them using a combination of comparisons and jumps. Two steps are involved in
executing a conditional statement: First, an operation such as CMP, AND, or SUB modifies
the CPU status flags. Second, a conditional jump instruction tests the flags and causes a
branch to a new address. Let’s look at a couple of examples.
 Example 1 The CMP instruction in the following example compares EAX to Zero. The JZ
(jump if Zero) instruction jumps to label L1 if the Zero flag was set by the CMP instruction:
 Example 2 The AND instruction in the following example performs a bitwise AND on the DL register,
affecting the Zero flag. The JNZ (jump if not Zero) instruction jumps if the Zero flag is clear:

Generally, the conditional jump instructions syntax is given as


follows:
• Jcond destination, cond refers to a flag condition
identifying the state of one or more flags. The following
examples are based on the Carry and Zero flags:
Types of Conditional Jump Instructions
 The x86 instruction set has a large number  Table below shows a list of jumps based on the Zero,
Carry, Overflow, Parity, and Sign flags.
of conditional jump instructions.
 They are able to compare signed and
unsigned integers and perform actions based
on the values of individual CPU flags.
 The conditional jump instructions can be
divided into four groups:
 Jumps based on specific flag values

 Jumps based on equality between

operands or the value of (E)CX


 Jumps based on comparisons of

unsigned operands
 Jumps based on comparisons of signed

operands
Equality Comparisons
 The table lists jump instructions based on
evaluating equality. In some cases, two operands
are compared; in other cases, a jump is taken Following are code examples that use the JE, JNE,
JCXZ, and JECXZ instructions. Examine the
based on the value of CX or ECX. In the table, the comments carefully to be sure that you understand
notations left Op and right Op refer to the left why the conditional jumps were (or were not) taken.
(destination) and right (source) operands in a CMP
instruction:
CMP left Op, right Op
 The operand names reflect the ordering of
operands for relational operators in algebra. For
example, in the expression X < Y, X is called left
Op and Y is called right Op.
Unsigned Comparisons
 Signed Comparisons Table below displays a list
 Jumps based on comparisons of unsigned numbers are
of jumps based on signed comparisons. The
shown in Table below. The operand names reflect the
following instruction sequence demonstrates the
order of operands, as in the expression (leftOp < rightOp).
comparison of two signed values:
The jumps in Table are only meaningful when comparing
unsigned values. Signed operands use a different set of
jumps.
Cont…
 In the following code examples, examine the comments to be sure you understand why the
jumps were (or were not) taken.
Conditional structures
 In assembly, it is possible to use different control structures
like high-level languages.
 Let’s start with the most common high-level control structure:
if-then-else in C or C++
 In the following C++ code, two assignment statements are
executed if op1 is equal to op2:
if( op1 == op2 ) then
{ We translate this IF statement into assembly language
with a CMP instruction followed by conditional jumps.
X = 1; Y = 2;
}
Cont..
 The following code implements the IF statement as efficiently as possible by allowing
the code to “fall through” to the two MOV instructions that we want to execute when the
Boolean condition is true:
mov ax,op1
if( op1 == op2 ) then cmp ax,op2 ;op1 == op2?
{ jne L1 ; no: skip next
mov X,1 ; yes: assign X and Y
X = 1; Y = 2; mov Y,2
} L1:

If we implemented the ==operator using JE, the resulting code would be slightly less compact (six instructions rather
than five):
mov ax,op1
cmp ax,op2 ;op1 == op2?
je L1 ; yes: jump to L1
jmp L2 ; no: skip assignments
L1: mov X,1 ; assign X and Y
mov Y,2
L2:
Cont…
 Nested IF statement
if op1 == op2 then
if X > Y then
call Routine1
else
call Routine2
end if
else
call Routine3
end if

The table shows the results of white box testing of


the sample code. In the first four columns, test
values have been assigned to op1, op2, X, and Y.
The resulting execution paths are verified in
columns 5 and 6.
Compound Expressions
 Logical AND Operator Assembly language easily implements compound Boolean expressions containing
AND operators.
 Consider the following pseudocode, in which the values being compared are assumed to be unsigned
integers:
Short-Circuit Evaluation The following is a straightforward implementation
 if (al > bl) AND (bl > cl) then
using short circuit evaluation, in which the second expression is not evaluated
 X=1
if the first expression is false. This is the norm for high-level languages:
 end if

We can reduce the code to five instructions by


changing the initial JA instruction to JBE:
Logical OR Operator
 When a compound expression contains subexpressions joined by the OR operator, the overall expression is
true if any of the subexpressions is true.
 Let’s use the following pseudocode as an example:

In the following implementation, the code branches to L1 if the first expression is true; otherwise, it falls
through to the second CMP instruction. The second expression reverses the > operator and uses JBE
instead:

 For a given compound expression there are multiple way of implementing in assembly langauge.
WHILE Loops
 A WHILE loop tests a condition first before performing a block of statements. As long as the
loop condition remains true, the statements are repeated.
 The following loop is written in C++:
When implementing this structure in assembly language, it is convenient to
reverse the loop condition and jump to endwhile if a condition becomes true.
Assuming that val1 and val2 are variables, we must copy one of them to a
register at the beginning and restore the variable’s value at the end:

EAX is a proxy (substitute) for val1 inside the loop. References to


val1 must be through EAX. JNL is used, implying that val1 and val2
are signed integers.
Cont…
 Example: IF statement Nested in a Loop High-level languages are particularly good at representing nested control
structures. In the following C++ code, an IF statement is nested inside a WHILE loop. It calculates the sum of all array
elements greater than the value in sample:
Decision Directive
 Using the .IF Directive
 Runtime Expressions
 Relational and Logical Operators
 MASM-Generated Code
 .REPEAT Directive
 .WHILE Directive
Runtime Expressions

 .IF, .ELSE, .ELSEIF, and .ENDIF can be used to


evaluate runtime expressions and create block-
structured IF statements.
 Examples

MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump
instructions.
Relational and Logical Operators
MASM-Generated Code

. . . unless you prefix one of the register operands with the SDWORD PTR operator.
Then a signed jump is generated.
.REPEAT Directive
 Executes the loop body before testing the loop
condition associated with the .UNTIL directive.
Example
.WHILE Directive
 Tests the loop condition before executing the loop
body The .ENDW directive marks the end of the loop.
Example:

You might also like