Assembly 04
Assembly 04
Defining Data
Symbolic Constants
.code
0 10001111 Source
mov bl,10001111b
movzx ax,bl ; zero-extension
10001111 Source
mov bl,10001111b
movsx ax,bl ; sign extension
.code
xchg ax,bx ; exchange 16-bit regs
xchg ah,al ; exchange 8-bit regs
xchg var1,bx ; exchange mem, reg
xchg eax,ebx ; exchange 32-bit regs
Match sizes
.data
.code
mov al,arrayB+1 ; AL = 20h
mov al,[arrayB+1] ; alternative notation
.code
mov ax,[arrayW+2] ; AX = 2000h
mov ax,[arrayW+4] ; AX = 3000h
mov eax,[arrayD+4] ; EAX = 00000002h
Solution:
mov al, myBytes
add al, [myBytes+1]
add al,[myBytes+2]
.data
.code
movzx ax,myBytes
mov bl,[myBytes+1]
add ax,bx
mov bl,[myBytes+2]
add ax,bx ; AX = sum
(True/False): The EIP register cannot be the destination operand of a MOV instruction.
INC destination
Logic: destination destination + 1
(e.g., destination++)
DEC destination
Logic: destination destination – 1
(e.g., destination--)
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
.code
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh
Lesson: You can make yourself really confused and your code becomes
garble if you keep using a register for different sized values (ax, al, eax, ah,
ax, …)
Pay attention to detail and know exactly what is in every part of a register
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767
.data
Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
;first term :-Xval
mov eax,Xval
neg eax ; EAX = -26
;second term :Yval – Zval
mov ebx,Yval
sub ebx,Zval ; EBX = -10
;add the terms and store the result
add eax,ebx
mov Rval,eax ; -36
Mahmoud El‐Gayyar / Assembly Language 26
Flags Affected by Arithmetic
The ALU has a number of status flags that reflect the
outcome of arithmetic (and bitwise) operations
based on the contents of the destination operand after the operation
Essential flags:
Zero flag – set when destination equals zero
CPU
part of executes
executes ALU
conditional jumps
arithmetic & bitwise used by
attached to
operations
provide
affect
status flags
branching logic
Remember...
• A flag is set when it equals 1
• A flag is clear when it equals 0
mov al,0FFh
add al,1 ; CF = 1, AL = 00
mov ax,0FFh
add ax,1 ; CF = 0, AX = 0100h
mov bh,6Ch
mov al,2
mov al,-2
add al,+127 ; OF = 0
Overflow never occurs when the signs of two addition operands are different.
Sub a, b add a, ‐b
Mahmoud El‐Gayyar / Assembly Language 38
Warning
How does the CPU know whether an arithmetic operation is
signed or unsigned?
2. (Yes/No): Is it possible to set the Overflow flag if you add a positive integer to a
negative integer?
3. (Yes/No): Is it possible for the NEG instruction to set the Overflow flag?
4. (Yes/No): Is it possible for both the Sign and Zero flags to be set at the same time?
5. Write a sequence of two instructions that set both the Carry and Overflow flags at
the same time.
data segment:
myByte
.code
// C++ version:
char array[1000];
char * p = array;
; Assembly language:
.data
array BYTE 1000 DUP(?)
.code
mov esi,OFFSET array
byte offset
78 0000
When integers are loaded from memory
56 0001
into registers, the bytes are automatically
34 0002
re‐reversed into their correct positions
12 0003
56 0001 myDouble + 1
12 0003 myDouble + 3
.data
myBytes BYTE 12h,34h,56h,78h
.code
.code
.code
.data LENGTHOF
byte1 BYTE 10,20,30 ; 3
.code
mov ecx,LENGTHOF array1 ; 32
.code
mov ecx,SIZEOF array1 ; 64
.code
mov eax,LENGTHOF array ; eax=6
.code
mov eax,LENGTHOF array ; 2
inc esi
mov al,[esi] ; AL = 20h NOTE: We tend to
use esi and edi to
inc esi store addresses
mov al,[esi] ; AL = 30h
Mahmoud El‐Gayyar / Assembly Language 60
Using PTR
Use PTR to clarify the size attribute of a memory operand
.code
mov esi,OFFSET myCount
.data
arrayB BYTE 0,1,2,3,4,5
arrayW WORD 0,1,2,3,4,5
arrayD DWORD 0,1,2,3,4,5
.code
mov esi,4
i.e., you can declare a pointer variable that contains the offset
of another variable.
.data
arrayW WORD 1000h,2000h,3000h
ptrW DWORD arrayW ; ptrW = offset of arrayW
; Alternative – same as above
;ptrW DWORD OFFSET arrayW
.code
mov esi,ptrW
A jump outside the current procedure must be to a special type of label called a
global label (which we will examine when we examine procedures)
Mahmoud El‐Gayyar / Assembly Language 68
LOOP Instruction
The LOOP instruction creates a counted loop using ECX
Logic:
ECX ECX – 1
if ECX != 0, jump back to target, else go to next instruction
mov ax,0
mov ecx,5
This loop calculates the sum:
L1: 5 + 4 + 3 +2 + 1
add ax,cx
loop L1
Mahmoud El‐Gayyar / Assembly Language 69
Examples
mov ax,6
mov ecx,4 ;Loop 4 times
L1:
inc ax ;Each iteration ax++ (7,8,9,10)
loop L1
In this example, the outer loop executes 100 times, and the inner loop 20 times
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2: .
.
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1 ; repeat the outer loop
.data
intarray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
.code
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
L1:
mov al,source[esi] ; get char from source
mov target[esi],al ; store it in the target
inc esi ; move to next character
2. (True/False): The LOOP instruction first checks to see whether ECX is not equal to
zero; then LOOP decrements ECX and jumps to the destination label.