Assembly Lap 4
Assembly Lap 4
Agenda
Basic Instructions
Status Flags
Addressing Modes
Branching (Unconditional & Conditional)
Loop
Compare Operands
Let’s remember …
10001111 source
00000000 10001111 dest
Zero Extension of Integers
MOVZX Instruction
It is only used with unsigned integers.
There are three variants:
1. MOVZX reg32, reg/mem8
2. MOVZX reg32, reg/mem16
3. MOVZX reg16, reg/mem8
Zero Extension of Integers
MOVZX Instruction
Example
.data
byteVal BYTE 10001111b
.code
movzx ax, byteVal ; AX = 0000000010001111b
Exercise
What Are Registers’ Values? (1)
INCLUDE Irvine32.inc
.code
main PROC
mov bx, 0A69Bh BX = A69Bh
movzx eax, bx EAX = 0000A69Bh
movzx edx, bl EDX = 0000009Bh
movzx cx, bl CX = 009Bh
exit
main ENDP
END main
Sign Extension of Integers
MOVSX Instruction
The MOVSX instruction (move with sign-extend) copies
the contents of a source operand into a destination
operand and sign-extends the value to 16 or 32 bits.
10001111 source
11111111 10001111 dest
Sign Extension of Integers
MOVSX Instruction
Example
.data
byteVal BYTE 10001111b
.code
movsx ax, byteVal ; AX = 1111111110001111b
Exercise
What Are Registers’ Values? (2)
INCLUDE Irvine32.inc
.code
main PROC
mov bx, 0A69Bh BX = A69Bh
movsx eax, bx EAX = FFFFA69Bh
movsx edx, bl EDX = FFFFFF9Bh
mov bl, 7Bh BL = 7Bh
movsx cx, bl CX = 007Bh
exit
main ENDP
END main
Very fast built
Examples
• xchg ax, bx ;exchange 16-bit registers
• xchg ah, al ;exchange 8-bit registers
• xchg var1, bx ;exchange 16-bit memory
operand with BX
• xchg eax, ebx ;exchange 32-bit registers
Exercise
What Are Registers’ Values? (3)
INCLUDE Irvine32.inc
.data
val1 WORD 1000h
val2 WORD 2000h
.code AX = 1000h
main PROC AX = 2000h,val2 = 1000h
mov ax, val1 val1 = 2000h
xchg ax, val2
mov val1, ax
exit
main ENDP
END main
Direct-Offset Operands
Adding a displacement to the name of a variable
(creating a direct-offset operand) is useful when it is
needed to access memory locations that may not have
explicit label. (ARRAYS!!)
Adding 1 to
the off`set
.data
of arrayB arrayB BYTE 10h, 20h , 30h, 40h, 50h
.code
Adding 2 to mov al, arrayB ;AL = 10h
the offset of mov al, [arrayB + 1] ;AL = 20h
arrayB
mov al, [arrayB + 2] ;AL = 30h
Direct-Offset Operands
Effective
arrayB + 1 Address
Dereferencing the
[arrayB + 1] expression to obtain
the contents of
memory at the
address
In other words
mov al, [arrayB + 20] there is no
index out of
range exception
:D
Exercise
What Are AL’ Values? (4)
INCLUDE Irvine32.inc
.data
arrayB BYTE 10h, 20h, 30h, 40h, 50h
.code
main PROC
mov al, arrayB AL = 10h
mov al, [arrayB + 1] AL = 20h
mov al, arrayB + 2 AL = 30h
exit
main ENDP
END main
Direct-Offset Operands
WORD Arrays
In an array of 16-bit words, the offset of each array
element is 2 bytes beyond the previous one.
Exercise
What are the AX’s values? (5)
Adding 2 to the
offset of arrayW INCLUDE Irvine32.inc
to access the .data
second element arrayW WORD 1000h, 2000h, 3000h
.code
main PROC
mov ax, arrayW AX = 1000h
mov ax, [arrayW + 2] AX = 2000h
exit
main ENDP
END main
Direct-Offset Operands
DWORD Arrays
In an array of 32-bit words, the offset of each array
element is 4 bytes beyond the previous one.
Exercise
What are the EAX’s values? (6)
INCLUDE Irvine32.inc
.data
Adding 4 to the arrayD DWORD 10000000h, 20000000h
offset of arrayD .code
to access the main PROC
second element
mov eax, arrayD EAX = 10000000h
mov eax, [arrayD + 4] EAX = 20000000h
exit
main ENDP
END main
Little
Here is a trick !! Indian
100 • 11
INCLUDE Irvine32.inc
.data 102 • 10
arrayD WORD 1011h, 2022h 103 • 22
.code
104 • 20
main PROC
movzx eax, arrayD
movzx eax, [arrayD + 1]
exit AX = 1011h
AX = 2210h
main ENDP
END main
Revision and new instructions
Examples
myWord = 1001h
.data BX = 1001h
myWord WORD 1000h BX = 1000h
.code
inc myWord
mov bx, myWord
dec bx
Addition and Subtraction NEW
NEG Instruction
The NEG (negate) instruction reverses the sign of a
number by converting the number to its two’s
complement.
The following operands are permitted:
NEG reg/mem
Hands On
Write the code that implements the following arithmetic
expression:
Rval = - Xval + (Yval - Zval)
where Xval = 26, Yval = 30, and Zval = 40
Question
Data types will
be Signed or
Un?
Hands On
expression: INCLUDE Irvine32.inc
.data
Rval = - Xval + Rval SDWORD ?
Xval SDWORD 26
(Yval - Zval) Yval SDWORD 30
Zval SDWORD 40
where Xval = 26, .code
Yval = 30, and Zval main PROC
mov eax, Xval
= 40 neg eax
mov ebx, Yval
sub ebx, Zval
add eax, ebx
mov Rval, eax
exit
main ENDP
END main
Zero
Overflow Sign
Status
Flags
Auxiliary
Carry
Carry
Flags Affected by Addition and
Subtraction
When executing arithmetic instructions, we often want
to know something about the result.
INCLUDE Irvine32.inc
.code
main PROC
mov al, 1
sub al, 2
; AL = FFh, CF = 1
exit
main ENDP
END main
Carry Flag Quirks
00000001 1
The sum (10h)
contains a 1 in bit
position 4 that was 00001111 0Fh
carried out of bit
position 3 AC 1 00010000 10h
Auxiliary Carry Flag
INCLUDE Irvine32.inc
.code
main PROC
mov al, 0Fh
add al, 1
; AC = 1
exit
main ENDP
END main
Parity Flag
Direct
Immediate
Offset
Addressing Addressing Addressing
Modes
Types
In-Direct Direct
Memory Memory
Addressing Addressing
1.Register Addressing
MOV AX, BX
2. Immediate Addressing
MOV EAX, 23
3. Direct Memory Addressing
MOV AX, X
4. Indirect Memory Addressing
Base‐Index
Addressing
4. Indirect Memory Addressing
The instruction
copies the byte that MOV CL, byteArr[2]
is distanced two MOV CL, [byteArr + 2]
bytes from byteArr
to CL MOV CL, byteArr + 2
Let’s change the sequence of the program and make use of
flags…
A transfer of control
Assembly language
(jump), or branch, is a
programs use conditional
way of altering the
instructions to implement
order in which
high-level statements such
statements are executed
as IF statements and loops
JMP
The JMP instruction causes an unconditional transfer to a
destination, identified by a code label that is translated
by the assembler into an offset (address).
JMP destination
When the CPU executes an unconditional transfer, the
offset of destination is moved into the instruction pointer
(EIP), causing execution to continue at the new location.
JMP
Jumps may be short (between –128 and +127 bytes),
near (between –32,768 and +32,767 bytes from the
instruction following the jump), or far (in a different code
segment).
When the 80386+ processors are in FLAT memory model,
short jumps range is from –128 to +127 bytes and near
jumps range is from –2 to +2 gigabytes.
JMP Example
Comment on this
INCLUDE Irvine32.inc program …
.data
LOOP destination
So expected before using any LOOP inst. We have to set
the ECX to a specific value
LOOP
The execution of the LOOP instruction
involves two steps:
INCLUDE Irvine32.inc
.code
main PROC
mov ax, 0 AX = 0
mov ecx, 5 ECX = 5
L1:
inc ax AX = 1 AX = 2 AX = 3 AX = 4 AX = 5
loop Ll ECX = 4 ECX = 3 ECX = 2 ECX = 1 ECX = 0
exit
main ENDP
END main
Tracing Quirks …
If CX is the loop
INCLUDE Irvine32.inc counter (in real-address
.code mode), it repeats 65,536
main PROC times
mov ax, 0 AX = 0
mov ecx, 0 ECX = 0
L1:
inc ax AX = 1 AX = 2
loop Ll ECX = FFFFFFFFh …
ECX = FFFFFFFEh
exit
main ENDP
END main The loop repeats
4,294,967,296
times
Example: Sum of Int Array
This example calculates the summation of an array. It first
gets the offset of the array and stores it in ESI register.
Then, initialize ECX by the length of the array. Within the
loop, it accumulates the current element of the array
then slides ESI to point to the next element of the array
and so on till the ECX becomes zero.
.data
Arr1DWORD10, 20, 30, 40, 50
sum_val DWORD?
.code
main PROC
Mov the
Mov the 1st value
Address
Notes
PTR is an operator that overrides the size of an operand. It is always
preceded by a Type (BYTE, WORD, DWORD…etc). In the instruction add
eax, DWORD PTR [esi], you can remove DWORD PTR as the assembler
will assume a default size equals to the size of the second operand which
in this case DWORD. If ax is used instead of eax, WORD size will be
assumed and so on.
TYPE operator retrieves the number of bytes allocated for each item in
the given array. For example, the instruction add esi, TYPE Arr1 adds 4 to
esi if Arr1 is DWORD array, adds 2 if Arr1 is WORD array and adds 1 if
Arr1 is BYTE array
CMP Instruction
The CMP instruction compares the destination operand
to the source operand.
It performs an implied subtraction of a source operand
from a destination operand.
Neither operand is modified.
CMP is a valuable tool for creating conditional logic
structures.
CMP Results ZF CF
Destination < source 0 1
Destination > source 0 0
Destination = source 1 0
CMP Instruction Examples
mov ax, 5 ZF = 0, CF = 1
cmp ax, 10
Comparison Jump
Conditional jump
An operation such as
instruction tests the
CMP, AND or SUB
flags and causes a
modifies the CPU
branch to a new
status flags
address
Conditional Jump Instructions
A conditional jump instruction branches to a destination
label when a status flag condition is true.
Jxxx destination
Jxxx destination
Conditional Jump Instructions
xxx expresses the relation tested.
The following table shows the conditional Jump
instructions.
Relation For Unsigned Data For Signed Data
Equal/Zero JE/JZ
Not Equal/ Not Zero JNE/ JNZ
Above/ Greater JA/JNBE JG/JNLE
Above or Equal/ JAE/JNB JGE/JNL
Greater or Equal
Below/ Less JB/JNAE JL/JNGE
Below or Equal/ JBE/JNA JLE/JNG
Less or Equal
Example, 2 Var Relation
This example accepts two integers from the user and
prints the relation between those integers: greater, less,
or equal.
INCLUDE Irvine32.inc
.data
strgreater byte "X is above than Y", 0
strless byte "X is below than Y", 0
strequal byte "X is equal to Y", 0
x dword ?
y dword ?
.code
main PROC ;An Irvine function that reads an 32-bit
unsigned
call ReadDec ;decimal integer from user and stores it
in EAX
mov x, eax ;save entered data to X.
call ReadDec
mov y, eax
below:
mov edx, offset strless ;handle below case call
writestring
jmp next
equal:
mov edx, offset strequal ;handle equal case call
writestring
jmp next