CS241 Computer Organization Spring 2015 IA-32 2-10 2015
Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add, imul, xor, shr, ) stack frame Read: CS:APP2 Chapter 3, sections 3.1 3.5 Quiz on 2s-complement & float today Lab#1 Datalab due Feb. 24, teams encouraged Exam#1 Thursday, Feb. 19, 8:00 pm
Carnegie Mellon Assembly Characteristics: Operations Perform arithmetic function on register or memory data Transfer data between memory and register Load data from memory into register Store register data into memory Transfer control Unconditional jumps to/from procedures Conditional branches
Move instruction: mov IA32 instructions:! movl moves an int (4 bytes) transfer reg reg, reg mem, mem reg Memory addressing:! D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D displacement 1, 2, or 4 bytes Rb Base register: Any of 8 integer registers Ri Index register: Any, except for %esp (or %ebp) S: Scale: 1, 2, 4, or 8
Data formats movl moves an int, long or pointer movb moves a byte, movw moves a word, etc. C Data type Assembly suffix Size (bytes) char Byte b 1 short Word w 2 int Double Word l 4 long int Double Word l 4 char * Double Word l 4 float Single Precision s 4 double Double Precision l 8 cf. Figure 3.1, p. 167
Carnegie Mellon Integer Registers (IA32) Origin (mostly obsolete) %eax %ax %ah %al accumulate general purpose %ecx %edx %ebx %esi %cx %dx %bx %si %ch %dh %bh %cl %dl %bl counter data base source index %edi %di destination index %esp %sp stack pointer %ebp %bp base pointer 16-bit virtual registers (backwards compatibility)
Carnegie Mellon Moving Data: IA32 Moving Data movx Source, Dest x in {b, w, l} movl Source, Dest: Move 4-byte long word movw Source, Dest: Move 2-byte word movb Source, Dest: Move 1-byte byte %eax %ecx %edx %ebx %esi %edi %esp %ebp Lots of these in typical code
Carnegie Mellon Moving Data: IA32 Moving Data movl Source, Dest: Operand Types Immediate: Constant integer data Example: $0x400, $-533 Like C constant, but prefixed with $ Encoded with 1, 2, or 4 bytes Register: One of 8 integer registers Example: %eax, %edx But %esp and %ebp reserved for special use Others have special uses for particular instructions Memory: 4 consecutive bytes of memory at address given by register Simplest example: (%eax) Various other address modes %eax %ecx %edx %ebx %esi %edi %esp %ebp
Carnegie Mellon movl Operand Combinations Source Dest Src,Dest C Analog Imm Reg Mem movl $0x4,%eax temp = 0x4; movl $-147,(%eax) *p = -147; movl Reg Reg Mem movl %eax,%edx movl %eax,(%edx) temp2 = temp1; *p = temp; Mem Reg movl (%eax),%edx temp = *p; Cannot do memory-memory transfer with a single instruction
Carnegie Mellon Simple Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movl (%ecx),%eax Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl 8(%ebp),%edx Displacement D(R)
Carnegie Mellon Address Computation Examples %edx %ecx 0xf000 0x100 Expression Address Computation Address 0x8(%edx) 0xf000 + 0x8 0xf008 (%edx,%ecx) 0xf000 + 0x100 0xf100 (%edx,%ecx,4) 0xf000 + 4*0x100 0xf400 0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080
Arithmetic & logic operations Instruction add sub imul xor or and sal (or shl) sar shr Description adds subtraction integer multiply exclusive or or and left shift arithmetic right shift logical right shift cf. Figure 3.7, p. 178
Memory layout of a process /* add 1 to x */ int main() { int x = 17; FP (%ebp) SP (%esp) x = x + 1; return 0; } PC
IA32/Linux Stack Frame! Current Stack Frame ( Top to Bottom) Argument build: Parameters for function about to call Local variables If can t keep in registers Saved register context Old frame pointer! Caller Stack Frame Return address Pushed by call instruction Arguments for this call Frame pointer %ebp Stack pointer %esp Caller Frame Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build
IA32 Stack! Region of memory managed with stack discipline! Grows toward lower addresses! Register %esp contains lowest stack address = address of top element Stack Pointer: %esp Stack Bottom Increasing Addresses Stack Grows Down Stack Top
Stack Frames! Contents Local variables Return information Temporary space! Management Space allocated when enter procedure Set-up code Frame Pointer: %ebp Stack Pointer: %esp Previous Frame Frame for proc Stack Top Deallocated when return Finish code
/* add1.c */ int main() { } int x = 17; x = x + 1; return 0; compile with gcc c S -m32 add1.c.file.text.globl main.type main: leal andl pushl pushl movl pushl subl movl addl movl addl popl popl leal ret.size.ident "add1.c" main, @function 4(%esp), %ecx $-16, %esp -4(%ecx) %ebp %esp, %ebp %ecx $16, %esp $17, -8(%ebp) $1, -8(%ebp) $0, %eax $16, %esp %ecx %ebp -4(%ecx), %esp main,.-main "GCC: (GNU) 4.1.2
/* add1.c */ int main() { } int x = 17; x = x + 1; return 0; address of x is Frame Pointer - 8.file "add1.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -8(%ebp) # x = 17 addl $1, -8(%ebp) # x++ movl $0, %eax # return 0 addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret.size main,.-main.ident "GCC: (GNU) 4.1.2
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file "tmain.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax addl %eax, -12(%ebp) movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file "tmain.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax # eax = y addl %eax, -12(%ebp) # x = eax + x movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = x + y; return 0; }.file addxy.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -8(%ebp), %eax # eax = y addl %eax, -12(%ebp) # x = eax + x movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax sall $3, %eax addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax # multiply by 8 is <<3 addl -8(%ebp), %eax movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp
variable address x FP 12 y FP - 8 int main() { int x = 17; int y = -2; x = 8*x + y; return 0; }.file "multby8.c".text.globl main.type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $16, %esp movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x sall $3, %eax # multiply by 8 is <<3 addl -8(%ebp), %eax # eax += y movl %eax, -12(%ebp) # x = eax movl $0, %eax # return 0 addl $16, %esp