CS241 Computer Organization Spring 2015 IA-32 2-102015 Outline - - PowerPoint PPT Presentation

▶
cs241 computer organization spring 2015
SMART_READER_LITE
LIVE PREVIEW

CS241 Computer Organization Spring 2015 IA-32 2-102015 Outline - - PowerPoint PPT Presentation

CS241 Computer Organization Spring 2015 IA-32 2-102015 Outline Review HW#3 and Quiz#1 More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add, imul, xor,


slide-1
SLIDE 1

CS241
 Computer Organization
 Spring 2015

IA-32 2-10–2015

slide-2
SLIDE 2

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

Outline

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

Move instruction: mov

slide-5
SLIDE 5

Data formats

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

movl moves an int, long or pointer movb moves a byte, movw moves a word, etc.

slide-6
SLIDE 6

Carnegie Mellon

Integer Registers (IA32)

%eax %ecx %edx %ebx %esi %edi %esp %ebp

%ax %cx %dx %bx %si %di %sp %bp %ah %ch %dh %bh %al %cl %dl %bl 16-bit virtual registers (backwards compatibility) general purpose

accumulate counter data base source index destination index

stack pointer base pointer Origin (mostly obsolete)

slide-7
SLIDE 7

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”

⬛ Lots of these in typical code

%eax %ecx %edx %ebx %esi %edi %esp %ebp

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Carnegie Mellon

movl Operand Combinations

Cannot do memory-memory transfer with a single instruction

movl Imm Reg Mem Reg Mem Reg Mem Reg Source Dest C Analog

movl $0x4,%eax temp = 0x4; movl $-147,(%eax) *p = -147; movl %eax,%edx temp2 = temp1; movl %eax,(%edx) *p = temp; movl (%eax),%edx temp = *p;

Src,Dest

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Arithmetic & logic operations

Instruction Description add adds sub subtraction imul integer multiply xor exclusive or

  • r
  • r

and and sal (or shl) left shift sar arithmetic right shift shr logical right shift

  • cf. Figure 3.7, p. 178
slide-13
SLIDE 13

Memory layout of a process

/* add 1 to x */ int main() { int x = 17; x = x + 1; return 0; } PC SP (%esp) FP (%ebp)

slide-14
SLIDE 14

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

Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame Frame pointer
 %ebp Stack pointer %esp

slide-15
SLIDE 15

Region of memory managed with stack discipline Grows toward lower addresses Register %esp contains 
 lowest stack address
 = address of “top” element

IA32 Stack

Stack Pointer: %esp

Stack Grows Down Increasing Addresses

Stack “Top” Stack “Bottom”

slide-16
SLIDE 16

Frame for proc

Frame Pointer: %ebp

Contents

■ Local variables ■ Return information ■ Temporary space

Management

■ Space allocated when

enter procedure

  • “Set-up” code

■ Deallocated when return

  • “Finish” code

Stack Frames

Stack Pointer: %esp

Previous Frame Stack “Top”

slide-17
SLIDE 17

/* add1.c */ int main() { int x = 17; x = x + 1; return 0; } compile with

gcc –c –S -m32 add1.c

.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) addl $1, -8(%ebp) movl $0, %eax addl $16, %esp popl %ecx popl %ebp leal

  • 4(%ecx), %esp

ret .size main, .-main .ident "GCC: (GNU) 4.1.2 …

slide-18
SLIDE 18

/* add1.c */ int main() { int x = 17; x = x + 1; return 0; }

.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 …

address of x is Frame Pointer - 8

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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 …

variable address x FP – 12 y FP - 8