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 Size (bytes) suffix 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 %ecx %cx %ch %cl counter general purpose %edx %dx %dh %dl data %ebx %bx %bh %bl base source %esi %si index destination %edi %di index stack %esp %sp pointer base %ebp %bp pointer 16-bit virtual registers (backwards compatibility)
Carnegie Mellon Moving Data: IA32 %eax %ecx ⬛ Moving Data %edx ▪ movx Source , Dest %ebx ▪ x in { b, w, l } %esi ▪ movl Source , Dest : %edi Move 4-byte “long word” %esp ▪ movw Source , Dest : %ebp Move 2-byte “word” ▪ movb Source , Dest : Move 1-byte “byte” ⬛ Lots of these in typical code
Carnegie Mellon Moving Data: IA32 %eax %ecx ⬛ Moving Data movl Source , Dest : %edx %ebx ⬛ Operand Types ▪ Immediate: Constant integer data %esi ▪ Example: $0x400 , $-533 %edi ▪ Like C constant, but prefixed with ‘$’ %esp ▪ Encoded with 1, 2, or 4 bytes ▪ Register: One of 8 integer registers %ebp ▪ 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”
Carnegie Mellon movl Operand Combinations Source Dest Src,Dest C Analog Reg movl $0x4,%eax temp = 0x4; Imm Mem movl $-147,(%eax) *p = -147; Reg movl %eax,%edx temp2 = temp1; movl Reg Mem movl %eax,(%edx) *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 ⬛ Displacement D(R) Mem[Reg[R]+D] ▪ Register R specifies start of memory region ▪ Constant displacement D specifies offset movl 8(%ebp),%edx
Carnegie Mellon Address Computation Examples %edx 0xf000 %ecx 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 Description add adds sub subtraction imul integer multiply xor exclusive or or or and and sal (or shl) left shift sar arithmetic right shift shr logical right shift cf. Figure 3.7, p. 178
Memory layout of a process FP (%ebp) /* add 1 to x */ int main() { SP (%esp) int x = 17; x = x + 1; return 0; } PC
IA32/Linux Stack Frame � Current Stack Frame (“Top” to Bottom) Caller “Argument build:” ■ Frame Parameters for function about to call Arguments Local variables ■ Return Addr Frame If can’t keep in registers pointer Old %ebp Saved register context %ebp ■ Saved Old frame pointer ■ Registers + � Caller Stack Frame Local Variables Return address ■ Pushed by call instruction Argument ■ Stack Build pointer Arguments for this call ■ %esp
IA32 Stack Stack “Bottom” � Region of memory managed with stack discipline Increasing � Grows toward lower Addresses addresses � Register %esp contains lowest stack address = address of “top” Stack Grows element Down Stack Pointer: %esp Stack “Top”
Stack Frames Previous � Contents Frame ■ Local variables ■ Return information Frame Pointer: %ebp Frame ■ Temporary space for proc � Management Stack Pointer: %esp ■ Space allocated when enter procedure Stack “Top” ● “Set-up” code ■ Deallocated when return ● “Finish” code
.file "add1.c" /* add1.c */ .text .globl main int main() { .type main, @function main: int x = 17; leal 4(%esp), %ecx andl $-16, %esp x = x + 1; pushl -4(%ecx) pushl %ebp return 0; movl %esp, %ebp pushl %ecx subl $16, %esp } movl $17, -8(%ebp) addl $1, -8(%ebp) compile with movl $0, %eax gcc –c –S -m32 add1.c addl $16, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret .size main, .-main .ident "GCC: (GNU) 4.1.2 …
.file "add1.c" /* add1.c */ .text int main() { .globl main .type main, @function int x = 17; main: leal 4(%esp), %ecx x = x + 1; andl $-16, %esp pushl -4(%ecx) pushl %ebp return 0; movl %esp, %ebp pushl %ecx } subl $16, %esp movl $17, -8(%ebp) # x = 17 address of x is addl $1, -8(%ebp) # x++ Frame Pointer - 8 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 …
.file "tmain.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx x = x + y; subl $16, %esp movl $17, -12(%ebp) # x = 17 return 0; 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 …
.file "tmain.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx x = x + y; subl $16, %esp movl $17, -12(%ebp) # x = 17 return 0; 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
.file “addxy.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx x = x + y; subl $16, %esp movl $17, -12(%ebp) # x = 17 return 0; 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
.file "multby8.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx subl $16, %esp x = 8*x + y; movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax return 0; sall $3, %eax addl -8(%ebp), %eax } movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp …
.file "multby8.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx subl $16, %esp x = 8*x + y; movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x return 0; sall $3, %eax addl -8(%ebp), %eax } movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp …
.file "multby8.c" variable address .text x FP – 12 .globl main .type main, @function y FP - 8 main: int main() { leal 4(%esp), %ecx andl $-16, %esp int x = 17; pushl -4(%ecx) pushl %ebp int y = -2; movl %esp, %ebp pushl %ecx subl $16, %esp x = 8*x + y; movl $17, -12(%ebp) # x = 17 movl $-2, -8(%ebp) # y = -2 movl -12(%ebp), %eax # eax = x return 0; sall $3, %eax # multiply by 8 is <<3 addl -8(%ebp), %eax } movl %eax, -12(%ebp) movl $0, %eax addl $16, %esp …
Recommend
More recommend