Download presentation
Presentation is loading. Please wait.
1
Assembly IA-32
2
Registers (Data) Bits 31…24 Bits 23…16 Bits 15…8 Bits 7…0 EAX AH AL AX
EBX BH BL BX ECX DH CL CX EDX DH DL DX
3
Registers (Pointer and Index)
Bits 31…24 Bits 23…16 Bits 15…8 Bits 7…0 ESI SI EDI DI ESP SP EBP BP
4
Registers (Control) – Flags & EIP
EIP – a 32bit register that points to the next instruction to be executed Record information on the most recently executed arithmetic (e.g add, sub, etc) or logical instruction (and, or, etc) Zero (ZF) – jz jnz Carry (CF) – jc jnc Overflow (OF) – jo jno Sign (SF) – js jns Parity (PF) – jp jnp
5
Flags (examples) mov AL, 15 add AL, 100 # SF = 0 (positive result)
sub AL, 100 # SF = 1 (negative result) mov EAX, 8 # ZF = 0 sub EAX, 8 # ZF = 1 mov EAX, 'a' cmp eax, 0 # ZF = 0 mov EAX, '\0' cmp EAX, 0 # ZF = 1 cmp EAX, EBX # ZF = 1 if EAX = EBX # for (i = 12# i != 0# i--) mov ECX, 12 # ECX = 12 loop: # loop body sub ECX, 1 # ECX = ECX - 1 jnz loop # Jump if ZF = 0
6
Registers (Segment) 16 bits: Not used in modern applications
CS (code segment) DS (data segment) SS (stack segment) ES (extra data segment) FS (extra data segment) GS (extra data segment) Not used in modern applications Applications now use the flat memory model (unsegmented)
7
Addressing modes (1) Immediate Register Indirect Address
Index Addressing mov eax, 0xab # eax = 171 hex: 0xab mov eax, 171 # eax = 171 hex: 0xab mov eax, esp # eax = esp mov eax, [esp] # eax = *esp mov reg, [reg + reg * scale + offset] #scale = 1, 2, 4, 8, mov eax, [ebx + esi] #eax = *(ebx + esi) mov eax, [ebx + esi * 4] #eax = *(ebx + esi * 4) mov eax, [ebx + esi * ] #eax = *(ebx + esi * )
8
Addressing modes (2) Memory set PTR directive
mov [eax], ebx # *eax = ebx mov [eax + 4], ecx # *(eax+4) = ecx mov [eax + ebx], edx # *(eax+ebx) = edx mov [eax ], 4 # Error: operand must have the size specified mov byte ptr [eax], 2 #*( ((char*)eax) ) = 2 mov word ptr [eax+2], 2 #*( ((short*)eax) ) = 2 mov dword ptr [eax+4], 2 #*( ((int*)eax) ) = 2 Data Type Size Byte 8 bits Word 16 bits Double Word 32 bits
9
Labels and Comments Comments in assembly start with # and are single line Labels, are used as jump targets, they define a source location <label_name>: infinite_loop: jmp Infinite_loop some_label: cmp eax, 0 je some_label #jumps to some_label if eax is 0
10
Exercise 1 Translate the following C code to assembly:
# r = (a + b) – (c + d) # r should be stored in eax, a,b,c,d values are on ebx, ecx, edx and esi respectively
11
Exercise 1 (Solution) Translate the following C code to assembly:
# r = (a + b) – (c + d) # r should be stored in eax, a,b,c,d values are on ebx, ecx, edx and esi respectively add ebx, ecx # (ebx = ebx + ecx) | (ebx = a + b) add edx, esi # (edx = edx + esi) | (edx = c + d) mov eax, ebx # eax = ebx | eax = a+b sub eax, edx # eax = eax - ebx | eax = (a+b)-(c+d)
12
Exercise 2 Translate the following C code to assembly: #int * p;
# p is on eax
13
Exercise 2 (Solution) Translate the following C code to assembly:
#int * p# #*p = 2+3# # p is on eax mov ebx, 2 # ebx = 2 add ebx, 3 # ebx = ebx + 3 mov [eax],ebx # *eax = ebx
14
Exercise 3 Translate the following C code to assembly: # int max#
# if(a >= b) # max = a# # else # max = b# # max should be stored on eax, a and b are on ebx and ecx respectively
15
Exercise 3 (Solution) Translate the following C code to assembly:
# int max# # if(a >= b) # max = a# # else # max = b# # max should be stored on eax, a and b are on ebx and ecx respectively cmp ebx, ecx # update_flags(ecx – ebx) jl max_ecx # go to max_ecx if ebx < ecx mov eax, ebx # eax = ebx jmp end # goto end max_ecx: mov eax, ecx # eax = ecx end:
16
Exercise 4 Translate the following C code to assembly: #int sum = 0
#for(int i = 0; i < 10; ++i) # sum+=i; # sum should be stored on eax
17
Exercise 4 (Solution) Translate the following C code to assembly:
#int sum = 0# #for(int i = 0# i < 10# ++i) # sum+=i# # sum should be stored on eax mov eax, 0 # eax = 0 #for mov ebx, 0 # ebx = 0 loop: cmp ebx, 10 # update_flags(10 - ebx) jnl end_loop # jump if not less add eax, ebx # eax = eax + ebx inc ebx # ebx++# jmp loop # goto loop end_loop:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.