IC220 a = function2(b, c, d); SlideSet #3: Procedures & } - - PowerPoint PPT Presentation

ic220
SMART_READER_LITE
LIVE PREVIEW

IC220 a = function2(b, c, d); SlideSet #3: Procedures & } - - PowerPoint PPT Presentation

Procedure Example & Terminology void function1() { int a, b, c, d; IC220 a = function2(b, c, d); SlideSet #3: Procedures & } Instruction Representation (Sections 2.8, 2.5, 2.10) int function2(int s, int t, int u) { int x,


slide-1
SLIDE 1

IC220 SlideSet #3: Procedures & Instruction Representation (Sections 2.8, 2.5, 2.10)

Procedure Example & Terminology

void function1() { int a, b, c, d; … a = function2(b, c, d); … } int function2(int s, int t, int u) { int x, y, z; … return x; }

Big Picture – Steps for Executing a Procedure

1. Place parameters where the callee procedure can access them

  • 2. Transfer control to the callee procedure
  • 3. (Maybe) Acquire the storage resources needed for the callee procedure
  • 4. Callee performs the desired task
  • 5. Place the result somewhere that the “caller” procedure can access it
  • 6. Return control to the point of origin (in caller)

Step #1: Placement of Parameters

  • Assigned Registers:
  • If more than eight are needed?
  • Parameters are not “saved” across procedure call
slide-2
SLIDE 2

Step #2: Transfer Control to the Procedure

  • bl –

– Branches to the procedure address AND links to return address

  • Link saved in register _____

– What exactly is saved? – Why do we need this?

Allows procedure to be called at __________ points in code, _________ times, each having a _________ return address Step #3: Acquire storage resources needed by callee

  • Suppose callee wants to use registers x20, x21 and x22

– But caller still expects them to have same value after the call – Solution:

  • At start of function call:

subi _____,_____, ____ // stur x20, [sp, ____ ] // stur x21, [sp, ____ ] // stur x22, [sp, ____ ] //

  • WARNING: unlike book examples, must move sp in increments of 16

Step #3 Storage Continued

Contents of register Contents of register Contents of register

Step #4: Callee Execution

  • Use parameters from _________________ and _________________

(setup by caller)

  • Temporary storage locations to use for computation:
  • 1. Temporary registers (x9-x15)
  • 2. Argument registers (x0-x7)

if…

  • 3. Other registers

but…

  • 4. What if still need more?
slide-3
SLIDE 3

Step #5: Place result where caller can get it

  • Placement of Result (return value)

– Must place result in appropriate register(s)

  • 64-bit result:
  • More than 64-bit result:
  • Example: desired result currently in x5, then:

Step #6: Return control to caller – Part A

  • Part I – Restore appropriate registers before returning from the procedure

– ldur x22, [sp, #0] // restore x22 for caller – ldur x21, [sp, #8] // restore x21 for caller – ldur x20, [sp, #16] // restore x20 for caller – addi sp, sp, ______ // adjust stack to delete our 3 items

  • Part II – Return to proper location in caller (return address)

– Jump to stored address of next instruction after procedure call – Explicit instruction: – Often written with this shorthand (not 100% the same, but almost):

Recap – Steps for Executing a Procedure

1. Place parameters where the callee procedure can access them

  • 2. Transfer control to the callee procedure
  • 3. (Maybe) Acquire the storage resources needed for the callee procedure
  • 4. Callee performs the desired task
  • 5. Place the result somewhere that the “caller” procedure can access it
  • 6. Return control to the point of origin (in caller)

Example – putting it all together

  • Write assembly for the following procedure

long dog (long m, long n) { long result = m + n + 7; return result; }

  • Call this function to compute dog(5, 10):

EX: 2-31 to 2-32

slide-4
SLIDE 4

Register Conventions

  • Register Convention – for “Preserved on Call” registers (like X20):
  • 1. If used, the callee must store and return values for these registers
  • 2. If not used, not saved

Nested Procedures

  • What if the callee wants to call another procedure – any problems?
  • Solution?
  • This also applies to recursive procedures

Nested Procedures

  • “Activation record” – part of stack holding procedures saved values and local variables
  • FP – points to first word of activation record for procedure (not required; we don’t use it)

Example – putting it all together (again)

  • Write assembly for the following procedure

long cloak (long n) { if (n == 0) return 100; else return (n*n + dagger(n-1)); }

  • Call this function to compute cloak(6):
slide-5
SLIDE 5

Example – putting it all together

cloak: sub sp, sp, #16 stur lr, [sp, #0] stur x20, [sp, #8] mov x20, x0 cbnz x0, Else mov x0, 100 b cloakExit Else: sub x0, x0, #1 bl dagger mul x1, x20, x20 add x0, x0, x1 cloakExit: ldur x20, [sp, #8] ldur lr, [sp, #0] add sp, sp, #16 br lr .size cloak, . - cloak long cloak (long n) { if (n == 0) return 100; else return (n*n + dagger(n-1)); }

What does that function do?

long cloak (long n) { if (n == 0) return 100; else return (n*n + dagger(n-1)); }

Best practices for nested functions

myFunction: subi sp, sp, #16 // 16 or more, always multiple of 16 stur lr, [sp, #0] // do this first! // save ‘preserved’ registers here (if using any) // possibly save arguments here // (recommended: in ‘preserved’ register. Alternate: onto stack) // body of function (possibly with branches to ‘myFunctionExit’ ... myFunctionExit: // reload ‘preserved’ registers’ (if used any) ldur lr, [sp, #0] addi sp, sp, #16 // must match subi at start! br lr // recommended: ONE location where you return // Tell debugger where function this function ends! (IC220 requirement) .size myFunction, . - myFunction

EX: 2-36 to 2-38

slide-6
SLIDE 6

Why/how gdb?

squares: sub sp, sp, #16 stur lr, [sp, #0] stur x20, [sp, #8] mov x20, x0 cbnz x0, Else mov x0, 100 b squaresExit Else: sub x0, x0, #1 bl squares mul x1, x20, x20 add x0, x0, x1 squaresExit: ldur x20, [sp, #8] ldur lr, [sp, #0] add sp, sp, #16 br lr .size squares, . - squares

Representing Instructions

  • Assembly language provides convenient symbolic representation

– Much easier than writing down numbers – Simplifications, e.g., destination first

  • Machine language is the underlying reality

– Each instruction is simply a number, the “machine code” – Realities, e.g., destination is no longer first

  • LEGv8/ARVv8 instructions

– Always encoded as 32-bit instruction word – Small number of formats provide all the details – Registers assigned a 5 bit number – Regularity!

Representing Instructions

  • How does the CPU actually know what to do?
  • What tradeoffs do we have to make?
  • Why does this work

mov x0, 57 but not this mov x0, 57000 ????

slide-7
SLIDE 7

For reference: hexadecimal review

  • Base 16

– Compact representation of bit strings – 4 bits per hex digit – Often indicated by 0x prefix

0000 4 0100 8 1000 c 1100 1 0001 5 0101 9 1001 d 1101 2 0010 6 0110 a 1010 e 1110 3 0011 7 0111 b 1011 f 1111

 Example: 0xeca8 6420 =

 1110 1100 1010 1000 0110 0100 0010 0000

LEGv8 R-format Instructions

  • R-format:

– For instructions involving 3 registers – add, adds, subs, and, orr, … – Example: add x9, x21, x3 0x458 3 21 9 Opcode Rm Shamt Rn Rd 11 bits 5 bits 6 bits 5 bits 5 bits

LEGv8 D-format Instructions

  • Load/store instructions (data transfer)

– Rt: destination (load) or source (store) register number – Example: ldur x9, [x10, #240]

  • Design Principle 3: Good design demands good

compromises

– Different formats complicate decoding, but allow 32-bit instructions uniformly – Keep formats as similar as possible

0x7c2 Opcode DT_Address Op2 Rn Rt 11 bits 9 bits 2 bits 5 bits 5 bits

LEGv8 I-format Instructions

  • Immediate instructions (addi, subi, andi, orri, ..)

– Immediate = “constant” – Rn: source register – Rd: destination register

  • Limitations on immediate field

– Immediate field is zero-extended – Immediate field has 12 bits

(somewhat obscure detail: actual ARMv8 limits are different because of option to shift the constant by exactly 12 bits)

Opcode Immediate Rn Rd 10 bits 12 bits 5 bits 5 bits

slide-8
SLIDE 8

LEGv8 Instruction Formats Summary

  • NOTE: this figure (from book) shows all values as DECIMAL, but “green

sheet” gives opcodes in HEX (easier to work with)

EX: 2-61 to 2-68

  • Most constants are small

– 12-bit immediate is sufficient

  • For the occasional bigger constant

MOVZ: move wide with zeros MOVK: move wide with keep

  • Both accept 16 bit immediates
  • And, use with “flexible 2nd operand” (shift)

0000 0000 0000 0000

Bigger Constants

MOVZ X9,255,LSL 16 MOVK X9,255,LSL 0

0000 0000 0000 0000 0000 0000 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 0000 0000 1111 1111

Branch Addressing

  • B-type

– B 10000 // go to location PC+10000

  • CB-type

– CBNZ X19, Exit // go to Exit if X19 != 0

  • Both addresses are PC-relative

– Address = PC + offset (from instruction) 5 10000ten

6 bits 26 bits

181 Exit

8 bits 19 bits

19

5 bits

LEGv8 Addressing Summary

slide-9
SLIDE 9

LEGv8 Encoding Summary