Top Banner
1 CPU Assembly Programmer’s View Programmer-Visible State PC: Program counter Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64) Register file Heavily used program data Condition codes Store status information about most recent PC Registers Memory Code Data Stack Addresses Data Instructi ons Condition Codes Memory Byte addressable array Code and user data Stack to support procedures
94

1 CPU Assembly Programmer’s View Programmer-Visible State PC: Program counter Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Jan 02, 2016

Download

Documents

Lisa Carpenter
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

1

CPU

Assembly Programmer’s View

Programmer-Visible State PC: Program counter

Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Register file Heavily used program data

Condition codes Store status information about most

recent arithmetic operation Used for conditional branching

PCRegisters

Memory

CodeDataStack

Addresses

Data

InstructionsConditionCodes

Memory Byte addressable array Code and user data Stack to support procedures

Page 2: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

2

Complete addressing mode andaddress computation (leal)

Page 3: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

3

Complete Memory Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]

D: Constant “displacement” 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp

Unlikely you’d use %ebp, either S: Scale: 1, 2, 4, or 8 (why these numbers?)

Special Cases (Rb,Ri)Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

Page 4: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

4

Address Computation Examples

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

%edx 0x7000

%ecx 0x0200

Expression Address Computation Address

0x8(%edx)

(%edx,%ecx)

(%edx,%ecx,4)

0x80(,%edx,2)

Page 5: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

5

Address Computation Instruction leal Src,Dest

Src is address mode expression Set Dest to address denoted by expression

Uses Computing addresses without a memory reference

E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y

k = 1, 2, 4, or 8 Example

int mul12(int x){ return x*12;}

int mul12(int x){ return x*12;}

leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2

leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2

Converted to ASM by compiler:

Page 6: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

6

Arithmetic operations

Page 7: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

7

Some Arithmetic Operations Two Operand Instructions:FormatComputationaddl Src,Dest Dest = Dest + Srcsubl Src,Dest Dest = Dest Srcimull Src,Dest Dest = Dest * Srcsall Src,Dest Dest = Dest << Src Also called shllsarl Src,Dest Dest = Dest >> Src Arithmeticshrl Src,Dest Dest = Dest >> Src Logicalxorl Src,Dest Dest = Dest ^ Srcandl Src,Dest Dest = Dest & Srcorl Src,Dest Dest = Dest | Src

Watch out for argument order! No distinction between signed and unsigned int (why?)

Page 8: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

8

Some Arithmetic Operations One Operand Instructionsincl Dest Dest = Dest + 1decl Dest Dest = Dest 1negl Dest Dest = Destnotl Dest Dest = ~Dest

See the chapter from CSAPP for more instructions

Page 9: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

9

Arithmetic Expression Example

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

arith:pushl %ebpmovl %esp, %ebp

movl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eax

popl %ebpret

Body

SetUp

Finish

Page 10: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

10

16 z

12 y

8 x

4 Rtn Addr

0 Old %ebp

Understanding arith

movl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eax

%ebp

Offsetint arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

Page 11: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

11

16 z

12 y

8 x

4 Rtn Addr

0 Old %ebp

Understanding arith

%ebp

Offset

Stack

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)

Page 12: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

12

Observations about arith Instructions in different

order from C code Some expressions require

multiple instructions Some instructions cover

multiple expressions Get exact same code when

compile: (x+y+z)*(x+4+48*y)

movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

Page 13: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

13

CPU

Assembly Programmer’s View

Programmer-Visible State PC: Program counter

Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Register file Heavily used program data

Condition codes Store status information about most

recent arithmetic operation Used for conditional branching

PCRegisters

Memory

CodeDataStack

Addresses

Data

InstructionsConditionCodes

Memory Byte addressable array Code and user data Stack to support procedures

Page 14: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

14

Control: Conditon codes

Page 15: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

15

Processor State (IA32, Partial) Information

about currently executing program Temporary data

( %eax, … ) Location of runtime stack

( %ebp,%esp ) Location of current code

control point( %eip, … )

Status of recent tests( CF, ZF, SF, OF )

%eip

General purposeregisters

Current stack top

Current stack frame

Instruction pointer

CF ZF SF OF Condition codes

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

Page 16: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

16

Condition Codes (Implicit Setting)

Single bit registersCF Carry Flag (for unsigned) SF Sign Flag (for signed)ZF Zero Flag OF Overflow Flag (for signed)

Implicitly set (think of it as side effect) by arithmetic operationsExample: addl/addq Src,Dest ↔ t = a+bCF set if carry out from most significant bit (unsigned overflow)ZF set if t == 0SF set if t < 0 (as signed)OF set if two’s-complement (signed) overflow(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

Not set by lea instruction

Page 17: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

17

Condition Codes (Explicit Setting: Compare)

Explicit Setting by Compare Instructioncmpl Src2, Src1cmpl b,a like computing a-b without setting destination

CF set if carry out from most significant bit (used for unsigned comparisons)ZF set if a == bSF set if (a-b) < 0 (as signed)OF set if two’s-complement (signed) overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

Page 18: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

18

Condition Codes (Explicit Setting: Test)

Explicit Setting by Test instructiontestl Src2, Src1testl b,a like computing a&b without setting destination

Sets condition codes based on value of Src1 & Src2Useful to have one of the operands be a mask

ZF set when a&b == 0SF set when a&b < 0

Page 19: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

19

Reading Condition Codes SetX Instructions

Set single byte based on combinations of condition codes

SetX Condition Descriptionsete ZF Equal / Zerosetne ~ZF Not Equal / Not Zerosets SF Negativesetns ~SF Nonnegativesetg ~(SF^OF)&~ZF Greater (Signed)

setge ~(SF^OF) Greater or Equal (Signed)

setl (SF^OF) Less (Signed)setle (SF^OF)|ZF Less or Equal (Signed)seta ~CF&~ZF Above (unsigned)setb CF Below (unsigned)

Page 20: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

20

movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eax

Reading Condition Codes (Cont.)

SetX Instructions: Set single byte based on combination of condition

codes One of 8 addressable byte

registers Does not alter remaining 3 bytes Typically use movzbl to finish jobint gt (int x, int y){ return x > y;}

int gt (int x, int y){ return x > y;}

Body

%eax %ah %al

%ecx %ch %cl

%edx %dh %dl

%ebx %bh %bl

%esi

%edi

%esp

%ebp

Page 21: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

21

Conditional branches and moves

Page 22: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

22

Conditional branches and moves

Page 23: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

23

Jumping jX Instructions

Jump to different part of code depending on condition codes

jX Condition Descriptionjmp 1 Unconditional

je ZF Equal / Zero

jne ~ZF Not Equal / Not Zero

js SF Negative

jns ~SF Nonnegative

jg ~(SF^OF)&~ZF Greater (Signed)

jge ~(SF^OF) Greater or Equal (Signed)

jl (SF^OF) Less (Signed)

jle (SF^OF)|ZF Less or Equal (Signed)

ja ~CF&~ZF Above (unsigned)

jb CF Below (unsigned)

Page 24: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

24

Conditional Branch Example

int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7

.L6:subl %edx, %eax

.L7:popl %ebpret

Body1

Setup

Finish

Body2b

Body2a

Page 25: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

25

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

C allows “goto” as means of transferring control Closer to machine-level

programming style Generally

considered bad coding style

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7

.L6:subl %edx, %eax

.L7:popl %ebpret

Body1

Setup

Finish

Body2b

Body2a

Page 26: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

26

GO TO statements considered harmful

Page 27: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

27

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7

.L6:subl %edx, %eax

.L7:popl %ebpret

Body1

Setup

Finish

Body2b

Body2a

Page 28: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

28

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7

.L6:subl %edx, %eax

.L7:popl %ebpret

Body1

Setup

Finish

Body2b

Body2a

Page 29: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

29

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7

.L6:subl %edx, %eax

.L7:popl %ebpret

Body1

Setup

Finish

Body2b

Body2a

Page 30: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

30

Loops

Page 31: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

31

C Codeint pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}

int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}

Goto Versionint pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}

int pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}

“Do-While” Loop Example

Count number of 1’s in argument x (“popcount”)

Use conditional branch to either continue looping or to exit loop

Page 32: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

32

Goto Version“Do-While” Loop Compilation

Registers:%edx x%ecx result

movl $0, %ecx # result = 0.L2: # loop:

movl %edx, %eaxandl $1, %eax # t = x & 1addl %eax, %ecx # result += tshrl %edx # x >>= 1jne .L2 # If !0, goto loop

int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}

int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}

Page 33: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

33

C Code

do Body while (Test);

do Body while (Test);

Goto Version

loop: Body if (Test) goto loop

loop: Body if (Test) goto loop

General “Do-While” Translation

Body:

Test returns integer = 0 interpreted as false ≠ 0 interpreted as true

{ Statement1; Statement2; … Statementn;}

Page 34: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

34

C Code Goto Version

“While” Loop Example

Is this code equivalent to the do-while version? Must jump out of loop if test fails

int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}

int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}

int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}

int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}

Page 35: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

35

While version

while (Test) Bodywhile (Test) Body

Do-While Version

if (!Test) goto done; do Body while(Test);done:

if (!Test) goto done; do Body while(Test);done:

General “While” Translation

Goto Version

if (!Test) goto done;loop: Body if (Test) goto loop;done:

if (!Test) goto done;loop: Body if (Test) goto loop;done:

Page 36: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

36

C Code

“For” Loop Example

Is this code equivalent to other versions?

#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}

#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}

Page 37: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

37

“For” Loop While Loop

for (Init; Test; Update )

Body

For Version

Init;

while (Test ) {

Body

Update;

}

While Version

Page 38: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

38

“For” Loop Form

for (Init; Test; Update )

Body

General Form

for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }

for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }

i = 0i = 0

i < WSIZEi < WSIZE

i++i++

{ unsigned mask = 1 << i; result += (x & mask) != 0;}

{ unsigned mask = 1 << i; result += (x & mask) != 0;}

Init

Test

Update

Body

Page 39: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

39

“For” Loop … Goto

for (Init; Test; Update )

Body

For Version

Init;

while (Test ) {

Body

Update;

}

While Version

Init; if (!Test) goto done; do Body Update while(Test);done:

Init; if (!Test) goto done; do Body Update while(Test);done:

Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:

Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:

Page 40: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

40

C Code

“For” Loop Conversion Example

Initial test can be optimized away

#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}

#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}

Goto Version

int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}

int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}

Init

!Test

Body

UpdateTest

Page 41: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

41

CPU

Assembly Programmer’s View

Programmer-Visible State PC: Program counter

Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Register file Heavily used program data

Condition codes Store status information about most

recent arithmetic operation Used for conditional branching

PCRegisters

Memory

CodeDataStack

Addresses

Data

InstructionsConditionCodes

Memory Byte addressable array Code and user data Stack to support procedures

Page 42: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

42

Summary So far

Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches & conditional moves Loops

Coming up! Switch statements Stack Call / return Procedure call discipline

Page 43: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

43

Today Switch statements IA 32 Procedures

Stack Structure Calling Conventions Illustrations of Recursion & Pointers

Page 44: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

44

IA32 Stack Region of memory

managed with stack discipline

Grows toward lower addresses

Register %esp contains

lowest stack address address of “top” elementStack Pointer: %esp

Stack GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom”

Page 45: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

45

IA32 Stack: Push pushl Src

Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp

-4

Stack GrowsDown

IncreasingAddresses

Stack “Bottom”

Stack Pointer: %esp

Stack “Top”

Page 46: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

46

Stack Pointer: %esp

Stack GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom”IA32 Stack: Pop

+4

Page 47: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

47

Procedure Control Flow Use stack to support procedure call and

return Procedure call: call label

Push return address on stack Jump to label

Return address: Address of the next instruction right after call Example from disassembly804854e: e8 3d 06 00 00 call 8048b90 <main>

8048553: 50 pushl %eax Return address = 0x8048553

Procedure return: ret Pop address from stack Jump to address

Page 48: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

48

0x8048553

0x104%esp

%eip

%esp

%eip 0x8048b90

0x108

0x10c

0x110

0x104

0x804854e

123

Procedure Call Example

0x108

0x10c

0x110

123

0x108

call 8048b90

804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax

804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax

%eip: program counter

Page 49: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

49

%esp

%eip

0x104

%esp

%eip0x804859

1

0x104

0x108

0x10c

0x110

0x8048553

123

Procedure Return Example

0x108

0x10c

0x110

123

ret

8048591: c3 ret8048591: c3 ret

0x108

0x8048553

0x8048553

%eip: program counter

Page 50: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

50

Stack-Based Languages Languages that support recursion

e.g., C, Pascal, Java Code must be “Reentrant”

Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation

Arguments Local variables Return pointer

Stack discipline State for given procedure needed for limited time

From when called to when return Callee returns before caller does

Stack allocated in Frames state for single procedure instantiation

Page 51: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

51

Call Chain Example

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

yoo

who

amI

amI

amI

ExampleCall Chain

amI

Procedure amI() is recursive

Page 52: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

52

Frame Pointer: %ebp

Stack Frames Contents

Local variables Return information Temporary space

Management Space allocated when enter procedure

“Set-up” code Deallocated when return

“Finish” code

Stack Pointer: %esp

Stack “Top”

Previous Frame

Frame for

proc

Page 53: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

53

Example

yoo

who

amI

amI

amI

amI

yoo%ebp

%esp

Stack

yoo

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

Page 54: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

54

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

Page 55: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

55

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 56: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

56

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

amI

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 57: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

57

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

amI

amI

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 58: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

58

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

amI

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 59: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

59

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 60: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

60

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

Page 61: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

61

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

amI

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

amI(…){ • • amI(); • •}

amI(…){ • • amI(); • •}

Page 62: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

62

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

yoo

who

yoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

who(…){ • • • amI(); • • • amI(); • • •}

who(…){ • • • amI(); • • • amI(); • • •}

Page 63: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

63

Example

yoo

who

amI

amI

amI

amI

yoo%ebp

%esp

Stack

yooyoo(…){ • • who(); • •}

yoo(…){ • • who(); • •}

Page 64: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

64

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

SavedRegisters

+Local

Variables

ArgumentBuild

Old %ebp

Arguments

CallerFrame

Frame pointer

%ebp

Stack pointer

%esp

Page 65: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

65

Revisiting swap

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

int course1 = 15213;int course2 = 18243;

void call_swap() { swap(&course1, &course2);}

int course1 = 15213;int course2 = 18243;

void call_swap() { swap(&course1, &course2);}

call_swap:• • •subl $8, %espmovl $course2, 4(%esp)movl $course1, (%esp)call swap• • •

call_swap:• • •subl $8, %espmovl $course2, 4(%esp)movl $course1, (%esp)call swap• • •

&course2

&course1

Rtn adr %esp

ResultingStack•

••

Calling swap from call_swap

%esp

%espsubl

call

Page 66: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

66

Revisiting swap

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:pushl %ebpmovl %esp, %ebppushl %ebx

movl 8(%ebp), %edxmovl 12(%ebp), %ecxmovl (%edx), %ebxmovl (%ecx), %eaxmovl %eax, (%edx)movl %ebx, (%ecx)

popl %ebxpopl %ebpret

Body

SetUp

Finish

Page 67: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

67

swap Setup #1

swap:

pushl %ebp

movl %esp,%ebp

pushl %ebx

Resulting Stack

&course2

&course1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp

%ebp•••

%esp

Page 68: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

68

swap Setup #2

swap:

pushl %ebp

movl %esp,%ebp

pushl %ebx

Resulting Stack

&course2

&course1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp%ebp

•••

%esp

Page 69: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

69

swap Setup #3

swap:

pushl %ebp

movl %esp,%ebp

pushl %ebx

Resulting Stack

&course2

&course1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

%espOld %ebx

Page 70: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

70

swap Body

movl 8(%ebp),%edx # get xpmovl 12(%ebp),%ecx # get yp. . .

Resulting Stack

&course2

&course1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

%espOld %ebx

Offset relative to %ebp

12

8

4

Page 71: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

71

swap FinishStack Before Finish

popl %ebxpopl %ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

%espOld %ebx

Resulting Stack

yp

xp

Rtn adr

•••

%ebp

%esp

Observation Saved and restored register %ebx Not so for %eax, %ecx, %edx

Page 72: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

72

Disassembled swap08048384 <swap>: 8048384: 55 push %ebp 8048385: 89 e5 mov %esp,%ebp 8048387: 53 push %ebx 8048388: 8b 55 08 mov 0x8(%ebp),%edx 804838b: 8b 4d 0c mov 0xc(%ebp),%ecx 804838e: 8b 1a mov (%edx),%ebx 8048390: 8b 01 mov (%ecx),%eax 8048392: 89 02 mov %eax,(%edx) 8048394: 89 19 mov %ebx,(%ecx) 8048396: 5b pop %ebx 8048397: 5d pop %ebp 8048398: c3 ret

80483b4: movl $0x8049658,0x4(%esp) # Copy &course2 80483bc: movl $0x8049654,(%esp) # Copy &course1 80483c3: call 8048384 <swap> # Call swap 80483c8: leave # Prepare to return 80483c9: ret # Return

Calling Code

Page 73: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

74

CPU

Assembly Programmer’s View

Programmer-Visible State PC: Program counter

Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Register file Heavily used program data

Condition codes Store status information about most

recent arithmetic operation Used for conditional branching

PCRegisters

Memory

CodeDataStack

Addresses

Data

InstructionsConditionCodes

Memory Byte addressable array Code and user data Stack to support procedures

Page 74: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

75

%esp

Creating and Initializing Local Variableint add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

Variable localx must be stored on stack Because: Need to create pointer to it

Compute pointer as -4(%ebp)

First part of add3

x

Rtn adrOld

%ebp%ebp 0

4

8

-4 localx = x

Unused-12

-8

-16

add3:pushl%ebpmovl %esp, %ebpsubl $24, %esp # Alloc. 24 bytesmovl 8(%ebp), %eaxmovl %eax, -4(%ebp)# Set localx to x

add3:pushl%ebpmovl %esp, %ebpsubl $24, %esp # Alloc. 24 bytesmovl 8(%ebp), %eaxmovl %eax, -4(%ebp)# Set localx to x -20

-24

Page 75: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

76

%esp

Creating Pointer as Argument

int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

Use leal instruction to compute address of localx

Middle part of add3

x

Rtn adrOld

%ebp%ebp 0

4

8

-4 localx

Unused-12

-8

-16

movl $3, 4(%esp) # 2nd arg = 3leal -4(%ebp), %eax# &localxmovl %eax, (%esp) # 1st arg = &localxcall incrk

movl $3, 4(%esp) # 2nd arg = 3leal -4(%ebp), %eax# &localxmovl %eax, (%esp) # 1st arg = &localxcall incrk

-20

-24

3 %esp+4

Page 76: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

77

%esp

Retrieving local variable

int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}

Retrieve localx from stack as return value

Final part of add3

x

Rtn adrOld

%ebp%ebp 0

4

8

-4 localx

Unused-12

-8

-16

movl -4(%ebp), %eax # Return val= localxleaveret

movl -4(%ebp), %eax # Return val= localxleaveret

-20

-24

Page 77: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

79

So what about these arrays?

int a[16];

char *c;c = (char *)malloc(256);

How are arrays actually represented in assembly?

Page 78: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

80

Basic Data Types Integral

Stored & operated on in general (integer) registers Signed vs. unsigned depends on instructions used

Intel ASM Bytes Cbyte b 1 [unsigned] charword w 2 [unsigned] shortdouble word l 4 [unsigned] intquad word q 8 [unsigned] long int (x86-64)

Floating Point Stored & operated on in floating point registers

Intel ASM Bytes CSingle s 4 floatDouble l 8 doubleExtended t 10/12/16 long double

Page 79: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

81

Array Allocation Basic Principle

T A[L]; Array of data type T and length L Contiguously allocated region of L * sizeof(T) bytes

char string[12];

x x + 12

int val[5];

x x + 4 x + 8 x + 12 x + 16 x + 20

double a[3];

x + 24x x + 8 x + 16

char *p[3];

x x + 8 x + 16 x + 24

x x + 4 x + 8 x + 12

IA32

x86-64

Page 80: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

82

Array Access Basic Principle

T A[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T*

Reference Type? Value?val[4] int 3val int * xval+1 int * x + 4&val[2] int * x + 8val[5] int ??*(val+1)int 5val + i int * x + 4i

int val[5]; 1 5 2 1 3

x x + 4 x + 8 x + 12 x + 16 x + 20

WATWAT

Page 81: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

83

Array Example

Declaration “zip_dig cmu” equivalent to “int cmu[5]” Example arrays were allocated in successive 20 byte blocks

Not guaranteed to happen in general

#define ZLEN 5typedef int zip_dig[ZLEN];

zip_dig cmu = { 1, 5, 2, 1, 3 };zip_dig mit = { 0, 2, 1, 3, 9 };zip_dig ucb = { 9, 4, 7, 2, 0 };

zip_dig cmu; 1 5 2 1 3

16 20 24 28 32 36

zip_dig mit; 0 2 1 3 9

36 40 44 48 52 56

zip_dig ucb; 9 4 7 2 0

56 60 64 68 72 76

Page 82: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

84

Array Access - Idea

Array start

4 element array of ints

%edx

%eaxOffset

Page 83: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

85

Array Accessing Example

Register %edx contains starting address of array

Register %eax contains array index

Desired digit at 4*%eax + %edx

Use memory reference (%edx,%eax,4)

int get_digit (zip_dig z, int dig){ return z[dig];}

# %edx = z # %eax = dig

movl (%edx,%eax,4),%eax # z[dig]

IA32

zip_dig cmu; 1 5 2 1 3

16 20 24 28 32 36

Page 84: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

86

# edx = zmovl $0, %eax # %eax = i

.L4: # loop:addl $1, (%edx,%eax,4) # z[i]++addl $1, %eax # i++cmpl $5, %eax # i:5jne .L4 # if !=, goto loop

Array Loop Example (IA32)

void zincr(zip_dig z) { int i; for (i = 0; i < ZLEN; i++) z[i]++;}

Page 85: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

87

Pointer Loop Example (IA32)void zincr_p(zip_dig z) { int *zend = z+ZLEN; do { (*z)++; z++; } while (z != zend); }

void zincr_v(zip_dig z) { void *vz = z; int i = 0; do { (*((int *) (vz+i)))++; i += ISIZE; } while (i != ISIZE*ZLEN);}

# edx = z = vzmovl $0, %eax # i = 0

.L8: # loop:addl $1, (%edx,%eax) # Increment vz+iaddl $4, %eax # i += 4cmpl $20, %eax # Compare i:20jne .L8 # if !=, goto loop

Page 86: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

88

How do we fit a 2D matrix into memory?

88

a b c

d e f

g h i

a b c

d e f

g h i

Row-major ordering

Q: How do we find cell (i,j)?WAT

Page 87: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

8989

Page 88: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

90

Nested Array Example

“zip_dig pgh[4]” equivalent to “int pgh[4][5]” Variable pgh: array of 4 elements, allocated contiguously Each element is an array of 5 int’s, allocated contiguously

Important: “Row-Major” ordering of all elements guaranteed

#define PCOUNT 4zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }};

zip_digpgh[4];

76 96 116 136 156

1 5 2 0 6 1 5 2 1 3 1 5 2 1 7 1 5 2 2 1

Page 89: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

91

Multidimensional (Nested) Arrays Declaration

T A[R][C]; 2D array of data type T R rows, C columns Type T element requires K bytes

Array Size R * C * K bytes

Arrangement Row-Major Ordering

A[0][0] A[0][C-1]

A[R-1][0]

• • •

• • • A[R-1][C-1]

•••

•••

int A[R][C];

• • •A[0][0]

A[0]

[C-1]• • •

A[1][0]

A[1]

[C-1]• • •

A[R-1][0]

A[R-1][C-1]

•  •  •

4*R*C Bytes

a b c

d e f

g h i

Page 90: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

92

• • •

Nested Array Row Access Row Vectors

A[i] is array of C elements Each element of type T requires K bytes Starting address A + i * (C * K)

• • •A[i][0]

A[i]

[C-1]

A[i]

• • •A

[R-1][0]

A[R-1][C-1]

A[R-1]

• • •

A

• • •A[0][0]

A[0]

[C-1]

A[0]

A+i*C*4 A+(R-1)*C*4

int A[R][C];

Page 91: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

93

Nested Array Row Access Code

Row Vector pgh[index] is array of 5 int’s Starting address pgh+20*index

IA32 Code Computes and returns address Compute as pgh + 4*(index+4*index)

int *get_pgh_zip(int index){ return pgh[index];}

# %eax = indexleal (%eax,%eax,4),%eax # 5 * indexleal pgh(,%eax,4),%eax # pgh + (20 * index)

#define PCOUNT 4zip_dig pgh[PCOUNT] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }};

Page 92: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

94

• • •

Nested Array Row Access Array Elements

A[i][j] is element of type T, which requires K bytes Address A + i * (C * K) + j * K = A + (i * C + j)* K

• • • • • •A[i][j]

A[i]

• • •A

[R-1][0]

A[R-1][C-1]

A[R-1]

• • •

A

• • •A[0][0]

A[0]

[C-1]

A[0]

A+i*C*4 A+(R-1)*C*4

int A[R][C];

A+i*C*4+j*4

Page 93: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

95

• • •

Nested Array Row Access Array Elements

A[i][j] is element of type T, which requires K bytes Address A + i * (C * K) + j * K = A + (i * C + j)* K

• • • • • •A[i][j]

A[i]

• • •A

[R-1][0]

A[R-1][C-1]

A[R-1]

• • •

A

• • •A[0][0]

A[0]

[C-1]

A[0]

A+i*C*4 A+(R-1)*C*4

int A[R][C];

A+i*C*4+j*4

A[i][j] ==

A + (i*C + j)*K

Page 94: 1 CPU Assembly Programmer’s View Programmer-Visible State  PC: Program counter  Address of next instruction  Called “EIP” (IA32) or “RIP” (x86-64)

100

CPU

Assembly Programmer’s View

Programmer-Visible State PC: Program counter

Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)

Register file Heavily used program data

Condition codes Store status information about most

recent arithmetic operation Used for conditional branching

PCRegisters

Memory

CodeDataStack

Addresses

Data

InstructionsConditionCodes

Memory Byte addressable array Code and user data Stack to support procedures