Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Outline
Multiplication
Division
FLAGS register
Branch Instructions
If statements
Loop instructions
2/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Multiplication
I mul is for unsigned integersI imul is for signed integersI 255 × 255 = 65025 if unsigned
= −1 if signedI FFh = 1111|1111
I as unsigned is 255I as signed is 1|1111111 = -1
I Two’s complement representationI first bit 1 means -; 0 means +I flip all the bitsI add 1
3/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
mul
I mul sourceI source can be register or memoryI the other operand is implicit
source other operand resultbyte AL AXword AX DX:AXdword EAX EDX:EAX
4/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
imul
I imul sourceI source can be register or memoryI the other operand is implicit
I imul dest, source
I imul dest, source1, source2
See Table 2.2 for details
5/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Division
I div is for unsigned integersI idiv is for signed integersI both work the same wayI div source
I source can be register or memory
source division quotient remainderbyte AX/source AL AHword (DX:AX)/source AX DXdword (EDX:EAX)/source EAX EDX
Do not forget to initialize DX or EDX
6/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
FLAGS register
I Contains various flagsI cmp a, b
I subtracts a − bI does not store the resultI sets flags
I For unsigned integersI ZF zero flagI CF carry flag
I For signed integersI ZF zero flagI OF overflow flag; 1 when the result overflowsI SF sign flag; 1 when the result is negative
7/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
cmp
I Unsigned integers
cmp a,ba-b ZF CF=0 1 0>0 0 0<0 0 1
I Signed integers
cmp a,ba-b ZF SF,OF=0 1>0 0 SF=O, SF=OF<0 0 SF=1, SF!=OF
8/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Branch Instructions
Unconditional branchesI jmp labelI call label
I unconditional branchI like goto label
Conditional branches
I jxx label
I check flagsI if true, branch (transfer execution control) to label
I otherwise, continue from the next statement
9/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
I jxx short labelI the jump is ±128 bytes from the current locationI advantage: the offset is 1 byte
I jxx near labelI the jump is to any location within a segmentI label is 32 bitI default, same as jxx label
I jxx word labelI 16-bit label
I jxx far labelI outside a segment
10/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Do cmp a,bThen
if signed unsigneda=b je jea!=b jne jnea<b jl , jnge jb, jnaea>b jg, jnle ja, jnbea>=b jge, jnl jae, jnb
For more instructions, see the text
11/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
If statementsi f (condition) {/* then block */}else {/* else block */}
Can be translated as;; code that sets flags;; e.g. cmp a,bjxx else_block;; code in then blockjmp end_if
else_block:;; code in else block
endi_if:
jxx is a suitable branch instruction12/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
i f (condition) {/* then block */}
Can be translated as
;; code that sets flags;; e.g. cmp a,bjxx end_if;; code in then block
endi_if:
jxx is a suitable branch instruction
13/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Examples
sum=0;i=i-1;i f (i>0) sum++;
Can be translated into
;; assume i is in ecxmov eax, 0 ;sum=0dec ecx ;i=i-1j z end_ifinc eax ;sum++
end_if:
14/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
i f (eax>=5)ebx=1
elseebx=2
Can be translated into
cmp eax, 5jge then_blockmov ebx, 2jmp next
then_block:mov ebx, 1
next:
15/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
...or into
cmp eax, 5jnz else_blockmov ebx, 1jmp end_ifjmp next
else_block:mov ebx, 2
next:
16/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Loop instructionsloop instruction
Example
sum = 0;for (i=10; i>0; i--)
sum += i;
Can be translated into
mov eax, 0 ;sum=0mov ecx, 10 ;ecx=10, loop counter
loop_start:add eax, ecx ;sum+=iloop loop_start ;ecx--, goto loop_start
17/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
Example
sum = 0;for (i=1; i<=10; i++)
sum += i;
Is the following a correct translation?
mov ebx, 1mov eax, 0 ;sum=0mov ecx, 10 ;ecx=10, loop counter
loop_start:add eax, ebx ;sum+=iinc ebxloop loop_start ;ecx--, goto loop_start
18/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
loop
I loop start_loop same asI decrement ecx by 1I if ecx!=0 goto start_loop
I loope start_loopI loopz start_loop same as
I decrement ecx by 1I if ecx!=0 and ZF==1 goto start_loop
I loopne start_loopI loopnz start_loop same as
I decrement ecx by 1I if ecx!=0 and ZF==0 goto start_loop
ZF unchanged if ecx=0
19/21
Multiplication Division FLAGS register Branch Instructions If statements Loop instructions
While loops
Example
while (condition) {/* body of the while loop */}
Can be translated into
while:;; code that sets flagsjxx end_while ;branch if false;; code in the while bodyjmp while
end_while:
20/21