Assembly IA-32 Registers (Data) Bits 31…24 Bits 23…16 Bits 15…8 Bits 7…0 AH AL EAX AX BH EBX BL BX DH ECX CL CX DH EDX DL DX Registers (Pointer and Index) Bits 31…24 Bits 23…16 Bits 15…8 Bits 7…0 ESI SI EDI DI ESP SP EBP BP 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 Flags (examples) mov sub EAX, 8 EAX, 8 # ZF = 0 # ZF = 1 mov cmp EAX, 'a' eax, 0 # ZF = 0 mov cmp EAX, '\0' 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 mov add AL, 15 AL, 100 # SF = 0 (positive result) mov sub AL, 15 AL, 100 # SF = 1 (negative result) Registers (Segment) • 16 bits: • • • • • • 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) Addressing modes (1) • Immediate mov eax, 0xab mov eax, 171 # eax = 171 hex: 0xab # eax = 171 hex: 0xab • Register Indirect Address mov eax, esp mov eax, [esp] # eax = esp # eax = *esp • Index Addressing mov reg, [reg + reg * scale + offset] mov eax, [ebx + esi] mov eax, [ebx + esi * 4] mov eax, [ebx + esi * 4 + 10] #scale = 1, 2, 4, 8, #eax = *(ebx + esi) #eax = *(ebx + esi * 4) #eax = *(ebx + esi * 4 + 10) Addressing modes (2) • Memory set mov [eax], ebx mov [eax + 4], ecx mov [eax + ebx], edx mov [eax ], 4 # *eax = ebx # *(eax+4) = ecx # *(eax+ebx) = edx # Error: operand must have the size specified • PTR directive mov byte ptr [eax], 2 mov word ptr [eax+2], 2 mov dword ptr [eax+4], 2 #*( ((char*)eax) ) = 2 #*( ((short*)eax) ) = 2 #*( ((int*)eax) ) = 2 Data Type Size Byte 8 bits Word 16 bits Double Word 32 bits 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 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 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 add edx, esi # (ebx = ebx + ecx) | (ebx = a + b) # (edx = edx + esi) | (edx = c + d) mov eax, ebx sub eax, edx # eax = ebx # eax = eax - ebx | eax = a+b | eax = (a+b)-(c+d) Exercise 2 Translate the following C code to assembly: #int * p; #*p = 2+3; # p is on eax Exercise 2 (Solution) Translate the following C code to assembly: #int * p# #*p = 2+3# # p is on eax mov ebx, 2 add ebx, 3 mov [eax],ebx # ebx = 2 # ebx = ebx + 3 # *eax = ebx 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 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 jl max_ecx mov eax, ebx jmp end # update_flags(ecx – ebx) # go to max_ecx if ebx < ecx # eax = ebx # goto end mov eax, ecx # eax = ecx max_ecx: end: 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 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 mov ebx, 0 # ebx = 0 #for loop: cmp ebx, 10 jnl end_loop add eax, ebx inc ebx jmp loop end_loop: # update_flags(10 - ebx) # jump if not less # eax = eax + ebx # ebx++# # goto loop
© Copyright 2026 Paperzz