Uploaded by Somaiah Masaudi

chap 4 Introduction to ALanguage6

advertisement
Chapter 4 : Introduction to Assembly
Language The Intel 8086 ISA
Instruction Set Architecture
Outline
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
2
Types of instructions
▪ Assignment statements (Mov): data transfer between processor’s registers and the main
memory
• Transfer CPU
Main Memory (reading);
• Transfer CPU
Main Memory (writing);
addition : AX
AX + data ;
▪ Arithmetic and logical instructions :
• Transactions between data and the accumulator AX.
• The result is placed in the accumulator.
• The data can be a constant or a value in a memory location.
subtraction : AX
increment : AX
decrement : AX
shift left, shift right
AX - data ;
AX + 1 ;
AX - 1 ;
▪ Comparison Instructions
• Comparison of AX register to a data and positioning indicators (Flags)
▪ Branching instructions
• The next instruction to execute in memory is indicated by the IP register
• Branching instructions can change the value of IP to execute another instruction (loops,
tests)
There are two types of branching:
Unconditional branching: IP
address of a new instruction
Conditional branching: If a condition is satisfied, then branching, otherwise pass to the next instruction
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
3
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
4
Arithmetic instructions (1)
▪ The basic arithmetic instructions are addition, subtraction, multiplication and division
that includes variants. Several addressing modes are possible
▪ Addition: ADD destination , source
; destination = destination + source
▪ Examples
ADD AH, [1100H] ;Adds the contents of the memory cell 1100H in AH (direct addressing)
ADD AH, [BX]
;Adds the contents of the cell pointed by BX in AH (based addressing)
ADD BYTE PTR [1200H], 05H ; adds 05H to content of the memory offset (immediate addressing)
▪ Destination can be a register or a memory location
▪ Source can be a register, memory location, or a constant
▪ Destination and source must be of the same size
▪ Memory-to-memory arithmetic is not allowed
▪ Examples
ADD Tab1, Tab2 ; not allowed
; will be replaced by:
MOV AX, Tab2
ADD Tab1, AX
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
5
Arithmetic instructions (2)
▪ Increment: INC
Destination
; Destination =Destination + 1
▪ Examples:
INC AX ; AX = AX + 1 (increment of 16 bits)
INC AL ; AL = AL + 1 (increments of 8 bits)
INC [SI] ; the contents of the memory cell pointed by SI is incremented
▪ Subtraction : SUB destination , source
▪ Examples :
SUB AX, BX
SUB AL, BH
SUB AL, [SI]
SUB [DI], AL
; destination = destination – source
; AX = AX - BX (Subtract 16-bit)
; AL = AL - BH (Subtract 8-bit)
; AL = AL - the contents of the memory cell pointed by SI
; the contents of the memory cell pointed by DI, Is subtracted from AL,
the result is set in the memory cell pointed by DI
▪ same restrictions than ADD instruction
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
6
Arithmetic instructions (3)
▪ Decrement: DEC Destination
; Destination =Destination - 1
▪ Examples:
DEC AX ; AX = AX - 1 (decrement of 16 bits)
DEC AL ; AL = AL - 1 (decrements of 8 bits)
DEC [SI] ; the contents of the memory cell pointed by SI is decremented
▪ Note: You can’t decrement an immediate value
▪ Negation: NEG Destination
; Destination = 0 – Destination
▪ Examples:
NEG AX
; AX = 0 - AX
NEG AL
; AL = 0 - AL
NEG [IF] ; [IF] = 0 - [IF]
Note: The indicators
affected by this
operation are: AF,
CF, OF, PF, SF, ZF
▪ Comparing : CMP Destination, Source
; Destination - Source
• It subtracts the source from destination, which can be a byte or a word
• The result is not set in the destination, because that instruction affects only the indicators
to be tested later with another conditional jump instruction (AF, CF, OF, PF, SF, ZF)
• Only effect the flags. For example after CMP AX, 5 we will have ZF = 1 if AX
contains the value 5, and ZF = 0 if AX is different from 5
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
7
Arithmetic instructions (4)
▪ Affected Flags (ADD and SUB)
For each of the following marked entries, show the values of the destination
operand and the six status flags:
mov al,0FFh
add al,1
sub al,1
mov al,+127
add al,1
mov al,26h
sub al,95h
1
–
; AL=-1
; AL= 00h C= 1 O= 0 S= 0 Z= 1 A= 1 P= 1
; AL= FFh C= 1 O= 0 S=1 Z=0 A= 1 P= 1
; AL=7Fh
; AL= -128C= 0 O= 1 S=1 Z=0 A= 1 P=0
; AL= 91h C= 1 O= 1 S=1 Z=0 A=0 P=0
0
0
1
0
0
0
1
0
0
1
0
0
1
1
0
0 26h (38)
1
1
0
1
1
1
0
0
0
1
0
0
1
1
0 26h (38)
+
8
1
0
0
1
0
1
0
1 95h (-107)
0
1
1
0
1
0
1
1 6Bh (107)
1
0
0
1
0
0
0
1 91h (-111)
1
0
0
1
0
0
0
1 91h (-111)
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
8
Arithmetic instructions (5)
▪ Addition:
; there is two cases :
MUL operand
▪ Performs the multiplication of the AL’s contents by an operand of 1 byte
▪ The result is placed in AX (16 bit result) if operands are multiplied on a byte
MUL Op8
; AL x Op8 → AX
▪ Performs the multiplication of the contents of AX by an operand of 2 bytes
▪ The result is placed in (DX:AX) (32 bit result) if operands are multiplied on a 2-byte
MUL Op16 ; AX x Op16 → DX:AX
▪ The operand Op cannot be immediate, it is either a register or a memory position
▪ If Op is memory position you must specify the size (byte or word)
MUL BL
MUL CX
MUL BYTE [BX]
MUL WORD [BX]
; AL x BL → AX
; AX x CX → DX:AX
; AL x (byte referenced by BX) → AX
; AX x (word referenced by BX) → DX :AX
▪ The flags O and C are positioned if the higher part of the result is not zero
▪ The higher part is AH for 8-bit multiplication and DX for 16-bit multiplication
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
9
Arithmetic instructions (6)
▪ Examples
1. MOV AL,51
MOV BL,32
MUL BL
; → AX = 51 × 32
3. MOV AL ,43
MOV BYTE PTR [1200H],28
MUL BYTE PTR [1200H] ; → AX = 43 × 28
2. MOV AX,4253
MOV BX,1689
MUL BX ; → (DX, AX) = 4253 × 1689
4. MOV AX,1234
MOV WORD PTR [1200H],5678
MUL WORD PTR [1200H]
; → (DX, AX) = 1234 × 5678
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
10
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
11
Shift and Rotation instructions (1)
SHL
; Shift Left instruction
▪ Shifting left 1 bit multiplies a number by 2
▪ The last bit shifted out from the left becomes the Carry Flag
▪ Shifting left n bits multiplies the operand by 2n (fast multiplication)
0
CF
Carry Flag
▪ Example : 5 * 22 = 20
MOV DL,5 ; DL = 00000101b
SHL DL,2
; DL = 00010100b = 20, Carry Flag = 0
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
12
Shift and Rotation instructions (2)
SHR
; Shift Right instruction
▪ Performs a logical right shift on the destination operand
▪ The last bit shifted out from the right becomes the Carry Flag
▪ Shifting right n bits divides the operand by 2n (fast division)
0
CF
▪ Example : (80 / 2)/4 = 10
MOV DL, 80 ; DL = 01010000b = 80
SHR DL, 1
; DL = 00101000b = 40, CF = 0
SHR DL, 2
; DL = 00001010b = 10, CF = 0
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
13
Shift and Rotation instructions (3)
SAR
; Shift Arithmetic Right
▪ Performs a right arithmetic shift on the destination operand
▪ Fills the newly created bit position with a copy of the sign bit
▪ SAR preserves the number's sign
CF
▪ Example
MOV DL,-80
SAR DL,1
SAR DL,2
; DL = 10110000b
; DL = 11011000b = -40, CF = 0
; DL = 11110110b = -10, CF = 0
SAL : Shift Arithmetic Left is identical to SHL
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
14
Shift and Rotation instructions (4)
ROL ; Rotate Left instruction
▪ Rotates each bit to the left, according to the count operand
▪ Highest bit is copied into the Carry Flag and into the Lowest Bit
▪ No bits are lost
▪ Example
MOV AL,11110000b
ROL AL,1
; AL = 11100001b, CF = 1
▪ Provide finer control
MOV DL,3Fh ; DL = 00111111b
over bits than HLL
ROL DL,4
; DL = 11110011b = F3h, CF = 1
CF
ROR ; Rotate Right instruction
▪ Rotates each bit to the right, according to the count operand
▪ Lowest bit is copied into the Carry flag and into the highest bit
▪ No bits are lost
▪ Example
MOV AL,11110000b
ROR AL, 1
; AL = 01111000b, CF = 0
MOV DL, 3Fh ; DL = 00111111b
ROR 4DL,
4
DL = F3h,
CF =ISA1
Chapter
: Introduction
to; Assembly
Language,
Dr. Tarek ALthami
CF
Other shift/rotate instr:
• RCL ; Rotate Carry Left
• RCR ; Rotate Carry Right
• SHLD ; Shift Left Double
• SHRD ; Shift Right Double
CCSE YANBU
15
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
16
Boolean Instructions (1)
AND destination, source ; Bitwise AND between each pair of matching bits
▪ Following operand combinations are allowed
AND R, R
▪ We use the following abbreviations:
INST: Instruction,
Off: Offset address
AND R, Adr
R: any Register,
Cste: given (Constant)
AND R, Cste
SR: Segment Register
disp: Displacement (Constant)
OR: Offset Register
Op: Operand
AND Cste, R
Adr: Address
SO: Source Operand
AND Cste, Cste
[Adr]: Memory content
DO: Destination Operand
▪ Operands can be 8 or 16 bits and they must be of the same size
▪ AND instruction is often used to clear selected bits
AND
cleared
AND
00111011
00001111
00001011
unchanged
▪ Forcing a bit at 0 without changing the other bits
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
17
Boolean Instructions (2)
OR destination, source ; Bitwise OR between each pair of matching bits
▪ Following operand combinations are allowed
OR R, R
OR R, Adr
OR R, Cste
OR Cste, R
OR Cste, Cste
OR
▪ Operands can be 8 or 16 bits and they must be of the same size
▪ OR instruction is often used to set selected bits
OR
set
00111011
11110000
11111011
unchanged
▪ Forcing a bit at 1 without changing the other bits
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
18
Boolean Instructions (3)
▪ Converting Characters to Uppercase
Use the AND instruction to clear bit 5
AND instruction can convert
characters to uppercase
'a'= 0 1 1 0 0 0 0 1 'b’ = 0 1 1 0 0 0 1 0
'A’= 0 1 0 0 0 0 0 1 'B’ = 0 1 0 0 0 0 1 0
MOV CX, LENGTHOF mystring
MOV SI, OFFSET mystring
L1: AND BYTE PTR [SI], 11011111b ;clear bit 5
INC SI
LOOP L1
▪ Converting Characters to Lowercase
Use the OR instruction to set bit 5
OR instruction can convert characters to
lowercase
'A'= 0 1 0 0 0 0 0 1 'B' = 0 1 0 0 0 0 1 0
'a'= 0 1 1 0 0 0 0 1 'b' = 0 1 1 0 0 0 1 0
MOV CX, LENGTHOF mystring
MOV SI, OFFSET mystring
L1: OR BYTE PTR [SI], 20h ; set bit 5
INC SI
LOOP L1
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
19
Boolean Instructions (4)
XOR destination, source ; Bitwise XOR between each pair of matching bits
▪ Following operand combinations are allowed
XOR R, R
XOR R, Adr
XOR R, Cste
XOR Cste, R
XOR Cste, Cste
XOR
▪ Operands can be 8 or 16 bits and they must be of the same size
▪ XOR instruction is often used to invert selected bits
XOR
inverted
00111011
11110000
11001011
unchanged
▪ Inversing a bit without changing the other bits
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
20
Boolean Instructions (5)
NOT destination
; Inverts all the bits in a destination operand
▪ Result is called the 1's complement
▪ Destination can be a register or memory
NOT R
NOT Adr
NOT
00111011
11000100
inverted
▪ None of the Flags is affected by the NOT instruction
Affected Status Flags
The six status flags are affected
1) Carry Flag: cleared by AND, OR, and XOR
2) Overflow Flag: cleared by AND, OR, and XOR
3) Sign Flag: Copy of the sign bit in result
4) Zero Flag: Set when result is zero
5) Parity Flag: Set when parity in least-significant byte is even
6) Auxiliary Flag: Undefined by AND, OR, and XOR
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
21
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
22
Branching Instructions (1)
▪ Branching : transfers the further execution of the program to a new position of the
code
▪ 3 types of branching are possible:
• unconditional branching
• conditional branching
• subroutine call interrupt
▪ Unconditional branching
JMP xyz, LOOP xyz Causes an unconditional jump to the line labeled xyz. The jump is
always performed
▪ Conditional branching
JZ , JNZ JE , JNE, JC, JO, JNO, …Conditional branching are determined by indicators
(flags) which are themselves positioned by the previous instructions
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
23
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
24
Unconditional Jump Instructions (1)
JMP destination
▪
▪
JMP is an unconditional jump to a destination instruction
A label is used to identify the destination address
▪
Example:
label :
INC AX
DEC BX
JMP label ; infinite loop
▪
▪
JMP provides an easy way to create a loop
Loop will continue endlessly unless we find a way to terminate it
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
25
Unconditional Jump Instructions (2)
▪
▪
▪
JMP causes the modification of the IP register: IP  IP + displacement
JMP adds to IP register, the number of bytes (distance) between the jump
instruction and its destination. For a back jump, the distance is negative (2‘s
complement code)
The used displacement value to reach a certain instruction is:
displacement = @ of referred instruction - @ of following instruction
▪ Example: The following program writes indefinitely the value 0 at 0140H. The first
instruction is located at 100H
0100
0103
0106
0107
B8 00 00
A3 01 40
EB FC
MOV AX, 0
MOV [140]
JMP 0103
xxx
; clear AX
; AX wrote to address 140
; branching to 103
; never executed instruction
The displacement is in this case equal to FCh : -4 (= 103h-107h)
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
26
Unconditional Jump Instructions (3)
LOOP destination
▪ The LOOP instruction creates a counting loop
▪ The loop statement works automatically with the CX register (counts the iterations)
▪ Logic:
CX  CX – 1
if CX != 0 ; jump to destination label
▪ Example: calculate the sum of integers from 1 to 10
MOV AX, 0
; sum = AX
MOV CX, 10
; count = CX
L1:
ADD AX, CX
; accumulate sum in AX
LOOP L1
; decrement CX until 0
Note: 1) The ':' character at the end of the label is required only if the label is alone on the line
2) LOOPNE ; continue if not zero
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
27
Unconditional Jump Instructions (4)
Your Turn
What will be the final value of AX
MOV AX,6
MOV CX,4
L1:
INC AX
LOOP L1
Solution: 10
How many times will the loop
execute?
MOV AX,1
MOV CX,0
L2:
DEC AX
LOOP L2
Solution: 216 = 65536
What will be the final value of AX?
Chapter 4 : Introduction to Assembly Language, ISA
Solution: same value 1
Dr. Tarek ALthami
CCSE YANBU
28
Unconditional Jump Instructions (5)
Nested LOOP
▪ If you need to code a loop within a loop, you must save the outer loop counter
(CX value)
▪ Example
data segment
count DD ?
Ends
code segment
MOV CX, 100
L1:
MOV count, CX
MOV CX, 20
L2:
XXX xx, yy
LOOP L2
MOV CX, count
LOOP L1
; set outer loop count to 100
; save outer loop count
; set inner loop count to 20
; any instruction
; repeat the inner loop
; restore outer loop count
; repeat the outer loop
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
29
Unconditional Jump Instructions (7)
Example1 : Summing an Integer Array
data segment
intArray DW 100h,200h,300h,400h,500h,600h
Ends
code segment
MOV SI, OFFSET intArray
; address of intArray
MOV CX, LENGTHOF intArray ; loop counter
MOV AX, 0
; clear the accumulator
L1:
ADD AX,[SI]
; accumulate sum in AX
; SI contains the @ of an array element (pointer)
ADD SI, 2
; point to next integer
LOOP L1
; repeat until CX = 0
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
30
Unconditional Jump Instructions (8)
Example2 : Summing an Integer Array
data segment
intArray DD 10000h,20000h,30000h,40000h,50000h,60000h
Ends
code segment
MOV SI, 0
MOV CX, LENGTHOF intArray
MOV AX, 0
L1:
ADD AX, intArray[SI*4]
INC SI
LOOP L1
Chapter 4 : Introduction to Assembly Language, ISA
; index of intArray
; loop counter
; clear the accumulator
; accumulate sum in AX
; SI is used as a scaled index
; increment index
; repeat until CX = 0
Dr. Tarek ALthami
CCSE YANBU
31
Unconditional Jump Instructions (9)
Example : Copying a String from source to target
data segment
source DB "This is the source string", 0
target DB SIZEOF source DUP(0) ; Good use of SIZEOF
Ends
code segment
MOV SI,0
MOV CX, SIZEOF source
L1:
MOV AL,source[SI]
MOV target[SI],AL
INC SI
LOOP L1
Chapter 4 : Introduction to Assembly Language, ISA
; index register
; loop counter
; get char from source
; store it in the target
; ESI is used to index source & target strings
; increment index
; loop for entire string
Dr. Tarek ALthami
CCSE YANBU
32
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
33
Conditional Jump Instructions (1)
Jcondition destination
; condition is the jump condition
▪ A conditional jump is executed only if some condition is satisfied, otherwise
execution continues sequentially to the next instruction
▪ The condition of the jump carries on the state of one (or more) status indicators
(flags) of the microprocessor
▪ Example :
▪ Types of Conditional Jump Instructions
• Jumps based on specific flags
• Jumps based on equality
• Jumps based on the value of CX
• Jumps based on unsigned comparisons
• Jumps based on signed comparisons
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
34
Conditional Jump Instructions (2)
▪ Jumps based on specific flags
Note: the indicators are positioned for the result of the last operation (by the ALU)
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
35
Conditional Jump Instructions (3)
Jumps Based on Signed Comparisons
Jumps Based on Unsigned Comparison
Jumps Based on Equality
JE is equivalent to JZ
JNE is equivalent to JNZ
▪ Conditional jump usually follow the CMP (comparison instruction)
Example : if(A != B) ≡ CMP A,B
JNZ executedCodeIfTrue
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
36
Conditional Jump Instructions (4)
▪ Example 1
▪ Example 2
Jump to L1 if unsigned AX is greater than Var1
CMP AX, Var1
JA L1
; JA condition
; CF = 0, ZF = 0
Jump to L1 if signed AX is greater than Var1
CMP AX, Var1
JG L1
; JG condition
; OF = SF, ZF = 0
▪ Example 3
Jump to L1 if signed AX is greater than or equal to Var1
CMP AX, Var1
JGE L1
; JGE condition
; OF = SF
▪ Example 4
Jump to label L1 if bits 0, 1, and 3 in AL are all set
AND AL,00001011b
; clear bits except 0,1,3
CMP AL,00001011b
; check bits 0,1,3
JE L1
; all set? jump to L1
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
37
Conditional Jump Instructions (5)
▪ Example : Add two signed numbers N1 and N2 located respectively to offsets 1100H and
1101H. The result will be stored at offset 1102H if positive, at offset 1103H if it’s negative and
at offset 1104H if it’s zero
MOV AL, [1100H]
ADD AL, [1101H]
JS negative
JZ zero
MOV [1102H], AL
JMP end
negative :
MOV [1103H], AL
JMP end
zero :
MOV [1104H], AL
end : ………
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
38
Conditional Jump Instructions (6)
Application1: Sequential Search
; Receives:
;
;
; Returns:
search PROC USES CX
JCXZ notfound
L1:
CMP [SI], AX
JE found
ADD SI, 4
LOOP L1
notfound: MOV SI, 0
found :
RET
search ENDP
Chapter 4 : Introduction to Assembly Language, ISA
SI = array address
CX = array size
AX = search value
SI = address of found element
; jump if CX=0
; array element = search value?
; yes? found element
; no? point to next array element
; if not found then SI = 0
; if found, SI = element address
Dr. Tarek ALthami
CCSE YANBU
39
Conditional Jump Instructions (7)
Application2: Locate the first zero value in an array
▪ If none is found, let SI point to last array element
Data SEGMENT
array DW -3,7,20,-50,10,0,40,4
Code SEGMENT
MOV SI, OFFSET array – 2
MOV CX, LENGTHOF array
L1:
ADD SI, 2
CMP WORD PTR [SI], 0
LOOPNE L1
JZ found
notfound:
...
found:
...
Chapter 4 : Introduction to Assembly Language, ISA
; start before first
; loop counter
; point to next element
; check for zero
; continue if not zero
; found zero
; SI points to last array value
; SI points to first zero value
Dr. Tarek ALthami
CCSE YANBU
40
Next ...
▪
▪
▪
▪
▪
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Jump Instructions
▪ Unconditional Jump Instructions
▪ Conditional Jump Instructions
▪ Translating Conditional Structures
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
41
Translating Conditional Structures(1)
▪ Block-Structured
IF Statements
▪ IF statement in high-level languages (such as C or Java)
• Boolean expression (evaluates to true or false)
• List of statements performed when the expression is true
• Optional list of statements performed when expression is false
▪ Example : Translate IF statements into assembly language
C Language
Assembly Language
if( var1 == var2 )
X = 1;
else
X = 2;
Chapter 4 : Introduction to Assembly Language, ISA
MOV AX, var1
CMP AX, var2
JNE elsepart
MOV X, 1
JMP next
elsepart: mov X, 2
next:
Dr. Tarek ALthami
CCSE YANBU
42
Translating Conditional Structures(2)
▪ Example : Implement the following IF in assembly language All variables are
signed integers
C Language
Assembly Language
if (var1 <= var2) {
var3 = 10;
}
else {
var3 = 6;
var4 = 7;
}
MOV AX, var1
CMP AX, var2
JLE ifpart
MOV var3, 6
MOV var4, 7
JMP next
ifpart: MOV var3, 10
next:
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
43
Translating Conditional Structures(3)
▪ Compound Expression with
AND
▪ HLLs use short-circuit evaluation for logical AND
▪ If first expression is false, second expression is skipped
if ((al > bl) && (bl > cl)) {X = 1;}
L1:
L2:
next:
; One Possible Implementation ...
CMP AL, BL
; first expression ...
JA L1
; unsigned comparison : jump if above
JMP next
CMP BL, cl
; second expression ...
JA L2
; unsigned comparison
JMP next
MOV X,1
; both are true
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
44
Translating Conditional Structures(4)
▪Compound Expression with
OR
▪ HLLs use short-circuit evaluation for logical OR
▪ If first expression is true, second expression is skipped
if ( (al > bl) || (bl > cl) ) {X = 1;}
L1:
next:
CMP AL, BL
JA L1
CMP BL, CL
JBE next
MOV X,1
Chapter 4 : Introduction to Assembly Language, ISA
; is AL > BL?
; yes, execute if part
; no: is BL > CL?
; no: skip if part
; set X to 1
Dr. Tarek ALthami
CCSE YANBU
45
Translating Conditional Structures(5)
▪ WHILE Loops
▪ IF statement followed by
▪ The body of the loop, followed by
▪ Unconditional jump to the top of the loop
while( AX < BX) { AX = AX + 1; }
• This is a possible implementation:
top:
CMP AX, BX
JAE next
INC AX
JMP top
; AX < BX ?
; false? then exit loop
; body of loop
; repeat the loop
next:
Chapter 4 : Introduction to Assembly Language, ISA
Dr. Tarek ALthami
CCSE YANBU
46
Translating Conditional Structures(6)
Yet Another Solution for
While
▪ Check the loop condition at the end of the loop
▪ No need for JMP, loop body is reduced by 1 instruction
while ( BX <= var1) {
BX = BX + 5;
var1 = var1 - 1
}
top:
next:
CMP BX,var1
JA next
ADD BX, 5
DEC var1
CMP BX, var1
JBE top
Chapter 4 : Introduction to Assembly Language, ISA
; BX <= var1?
; false? exit loop
; execute body of loop
; BX <= var1?
; true? Repeat the loop
Dr. Tarek ALthami
CCSE YANBU
47
Summary
▪ Arithmetic
ADD, SUB, INC, DEC, NEG, …
▪ Shift and Rotation instructions
SHL, SHR, SAL, SAR,…
▪ Bitwise instructions (manipulate individual bits in operands)
AND, OR, XOR, NOT, …
▪ CMP: compares operands, sets condition flags for later conditional jumps and loops
▪ Conditional Jumps & Loops
Flag values: JZ, JNZ, JC, JNC, JO, JNO, JS, JNS,…
Equality: JE, JZ, JNE, JNZ,…
Signed: JG, JGE,…
Unsigned: JA, JAE, JB, JBE,…
LOOP, LOOPNZ
▪ JMP and LOOP Applications
Traversing and summing arrays, Computing the Max, Sequential Search
▪ Translating Conditional Structures
if…else, while, isDigit,…
Download