IA32 (Pentium) Processor Architecture Processor modes: 1. Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2. Virtual 8086 modes 3. Real mode – 1MB address space 4. System management mode Registers • 32-bit GPR’s (“general” purpose registers): eax ebx ecx edx eflags ebp esp esi edi eip Registers e[a,b,c,d]x: 31 eax 0 32 bits 15 ax 0 16 bits 7 ah 07 al 0 8 bits Note: eax is one register that can be viewed four different ways. (It is not four different registers. Schizophrenic?) Registers • Not really GPR’s. – eax - accumulator; multiplication and division – ecx - loop counter – esp - stack pointers; don’t use – esi, edi - for memory-to-memory transfer – ebp - used by HLL for local vars on stack Registers • Additional registers: – 16-bit segment registers • cs, es, ss, fs, ds, gs • don’t use – eip • instruction pointer / program counter (PC) • don‘t use Registers • Additional registers: – eflags • contains results of operations • 32 individual bits – control flags – status flags: » C = carry (unsigned) » O = overflow (signed); also called V » S = sign; also called N for negative » Z = zero Registers • Additional registers: – floating point registers: • ST(0) … ST(7) – MMX has 8 64-bit regs – XMM has 8 128-bit regs Fundamental instructions 1. mov - move 2. add - addition 3. sub - subtraction 4. call - call a function 5. ret - return from a function to caller Fundamental instructions • In all of the following examples, let: K equ a dword 12 52 a constant (doesn’t use memory) a variable (uses memory) Fundamental instructions MOV (MOVE) Fundamental instructions • mov - move – destination = source mov dst, src From IA-32 Intel Architecture Software Developer’s Manual, Volume 2A: Instruction Set Reference A-M mov mov r32, r/m32 Ex. mov ebx, ecx Ex. mov ebx, a mov r/m32, r32 Ex. mov ecx, ebx Ex. mov a, ebx mov r32, imm32 Ex. mov ebx, K mov r/m32, imm32 Ex. mov eax, K Ex. mov a, K mov notes mov r32, r/m32 Ex. mov Ex. mov ebx, ecx ebx, a mov r/m32, r32 Ex. mov Ex. mov ecx, ebx a, ebx mov r32, imm32 Ex. mov ebx, K mov r/m32, imm32 Ex. mov Ex. mov eax, K a, K • Note the duplication: mov eax, ebx appear 2x mov eax, K appears 2x. • Note: No mov m32, m32. • Note: No imm32, imm32. Why not? • Flags affected: None. – Will mov eax, 0 affect the Z flag? Problem: swap • Swap the contents of memory location A with the contents of memory location B (and vice versa). – Assume that A and B are dwords that are already defined for you. Fundamental instructions ADD Fundamental instructions • add – addition – Two add instructions (add and add w/ carry): • add dst, src dst = dst + src • adc dst, src dst = dst + src + CF • We will only use add. (advanced) From IA-32 Intel Architecture Software Developer’s Manual, Volume 2A: Instruction Set Reference A-M add add Ex. add Ex. Ex. add Ex. Ex. add Ex. Ex. eax, imm32 add eax, K r/m32, imm32 add ebx, K add a, K r/m32, r32 add ebx, ecx add a, ecx r32, r/m32 add ebx, ecx add ebx, a add notes add Ex. add Ex. Ex. add Ex. Ex. add Ex. Ex. eax, imm32 add eax, K r/m32, imm32 add ebx, K add a, K r/m32, r32 add ebx, ecx add a, ecx r32, r/m32 add ebx, ecx add ebx, a • Remember: eax is the accumulator. • Note the duplication. • Note: No add m32, m32. • Flags affected: O, S, Z, C • Would you call this a load/store architecture? Problem: add ‘em up • You are given 3 dwords, A, B, and C that have been previously assigned values. • Add them up and save the result in a dword called SUM. Fundamental instructions SUB Fundamental instructions • sub – subtraction – Like add, there are two subtract instructions, sub, and subtract w/ borrow: • sub dst, src dst = dst - src • sbb dst, src dst = dst – (src + CF) • We will only use sub. (advanced) From IA-32 Intel Architecture Software Developer’s Manual, Volume 2B: Instruction Set Reference N-Z sub sub Ex. sub Ex. Ex. sub Ex. Ex. sub Ex. Ex. eax, imm32 sub eax, K r/m32, imm32 sub ebx, K sub a, K r/m32, r32 sub ebx, ecx sub a, ecx r32, r/m32 sub ebx, ecx sub ebx, a sub notes sub Ex. sub Ex. Ex. sub Ex. Ex. sub Ex. Ex. eax, imm32 sub eax, K r/m32, imm32 sub ebx, K sub a, K r/m32, r32 sub ebx, ecx sub a, ecx r32, r/m32 sub ebx, ecx sub ebx, a • Remember: eax is the accumulator. • Note the duplication. • Note: No sub m32, m32. • Flags affected: O, S, Z, C • Would you call this a load/store architecture? Problem: subtract ‘em • You are given 3 dwords, A, B, and C that have been previously assigned values. • Subtract one from the first, two from the second, and ten from the third. • Would your solution still work if this were a load/store architecture? Fundamental instructions CALL Fundamental instructions • call - call a function (AKA subroutine, routine, procedure) – Use registers or stack to pass arguments to function. – Use registers to return value from function. – Ex. mov call mov eax, 0 f ebx, eax ;input param to function f is 0 in eax ;call our function ;save value returned for future use Fundamental instructions • Windows calling conventions: 1. Always assume that all flags are affected. 2. 32-bit Windows calling conventions: a) EBX, ESI, and EDI are always preserved by called function. b) EAX, ECX, and EDX can be freely modified (by called function). From IA-32 Intel Architecture Software Developer’s Manual, Volume 2A: Instruction Set Reference A-M Call example • The dump function can be called at any time by your program to display the contents of registers (including the EFLAGS register). • Unlike other Windows functions, it preserves all of the caller’s registers. Call example ;---------------------------------------------------------------------align 4 .code ;insert executable instructions below main PROC ;program execution begins here mov eax, 1 ;set regs values mov ebx, 2 mov ecx, 3 mov edx, 4 mov esi, 5 mov edi, 6 call dump ;show contents of regs mov eax, input(prompt) ;prompt the user exit ;end of program main ENDP ;---------------------------------------------------------------------align 4 .code ;insert executable instructions below main PROC ;program execution begins here mov eax, 1 ;set regs values mov ebx, 2 mov ecx, 3 mov edx, 4 mov esi, 5 mov edi, 6 call dump ;show contents of regs mov eax, input(prompt) ;prompt the user exit ;end of program main ENDP Call example ret • Return from procedure (AKA function, method, routine, or subroutine). • Flags affected: none. Problems: • One way to pass (and return) arguments to functions is to use registers. 1. Write a function that doubles a number. • The number to be doubled is in eax. • The result should also be in eax. 2. Write a function that adds three numbers together.