CS 2110 Quiz 3
.orig x3000 ;;do something HALT HELLO .fill x6000 .end .orig x6000 .stringz "Hi" .end *Load the letter i into register 1*
.orig x3000 LD R0, HELLO LDR R1, R0, 1 HALT HELLO .fill x6000 .end .orig x6000 .stringz "Hi" .end *Load the letter i into register 1*
TrapVect8
0000 to 00FF
what are the steps of stack buildup
1. Function A pushes any parameters that B takes in reverse order (A is global) 2. RV is a space in memory then allocated for the return value of B (not the return address) 3. store A's return address 4. Save A's Frame Pointer 5. Save a spot for a local variable in case R0-R4 is not enough 6. Save the rest of the registers
1. Function A pushes any parameters that B takes in reverse order (A is global) 2. RV is a space in memory then allocated for the return value of B (not the return address) 3. store A's return address 4. Save A's Frame Pointer 5. Save a spot for a local variable in case R0-R4 is not enough 6. Save R5 to R7 on the stack You just did all of this when calling a function. Now you are done. What happens next?
1. Save the return value 2. Restore values in R0 and R4 3. Restore R5 4. Retrieve the return address for A and put it into R7 so that we can jump back to function A 5. Increment the stack pointer so that the top of stack points to the return value of B 6. A takes this value, pops it off . A also pops of the parameters to B. Then we can continue to execute A. == Stack TearDown :D
1. Caller pushes arguments in reverse order 2. Caller uses JSR or JSRR to call a subroutine What happens next?
3. Callee stores RV, RA, old R5, at least one local variable. 4. Callee sets FP = SP 5. Callee allocates space for locals Set R6 to point to the return value Load R7 with the return address
Each trap instruction has a corresponding
8 bit trap vector we sign extend this this gives us an index into the trap vector table
What is the stack?
A dynamic section of memory that allows for local variables to exist for only the lifetime of a subroutine
You are tearing down a stack and have n arguments. How do you pop off these arguments from the stack.
ADD R6, R6, #(n+1)
How could I add 25 to the value in a register?
Add twice or .fill LD R1, TWO LD R2, THREE ADD R2, R2, R1 ADD R0, R2, #0 OUT TRAP x25 TWO .fill x32 THREE .fill x25
Remember that labels are an assembler shorthand for offsets. How do we translate labels into hexadecimal?
Address - PC*
Say I have a LD instruction at address x3000. What is the lowest memory address it can load from? The highest?
All data is stored x3000 to xFDFF Highest in one line 30FF
Who saves the RV (space)
Callee
Who saves the first local
Callee
Who saves the last local
Callee
Who saves the old F5
Callee
Who saves Argument 1 and 2
Caller
How to get return value after function finishes executing?
Caller pops the value off of the stack First value it pops off is return value Everything else is the arguments it passed in, in reverse order
How do you push a value onto the stack?
Decrement R6 STR R0, R6, 0
What is the instruction's opcode? What do DR/SR1/SR2/BaseR represent? What do imm5, offset6, PCoffset9, PCoffset11, and trapvect8 mean?
Dr/Sr1/Sr2/BaseR are all registers; others are all good
What are some helpful traps?
Halt Out Puts GetC
.orig x3000 AND R0, R0, 0 ST R0, ANSWER HALT .end ANSWER .blkw 1 Is this valid code?
I don't think this will assemble b/c ANSWER comes after END If we assume it is valid, then it puts the value in R0 (which is 0) and stores it at the address represented by ANSWER
JSR
Jump to Subroutine Saves the PC* value into R7 Sets PC to target = PC* + Offset11 (Have to save the PC before changing the PC)
JSRR
Jump to Subroutine, Register Uses a register for the subroutine's address instead of a label or offset R7 = PC* PC = BaseR
Addressing modes: LD, LDI, LDR
LD: pc-relative LDI: indirect LDR: base+offset
How do you pop a value off of the stack?
LDR R0, R6, 0 Increment R6
Labels are accepted by
PC-Relative Instructions (ST and LD)
What information is considered important or relevant to a subroutine?
Parameters (arguments) Return value Return address Old frame pointer At least one local variable Save old register values as well
IN
Print prompt to console, read and echo character from keyboard x23
Order of important info stored in a stack frame
R4 R3 R2 R1 R0 local <- R5 old R5 old R7 Return Value stored here 1st param passed into function (in reverse order)
You are given this stack 1. saved registeres 2. local variable x 3. old R5 4. old R7 5. Return Value 6. Parameter for function You just completed the function and need to save the return value. How do you get to this address?
R5 + 3
where is the subroutine's return value
R5 + 3
where is the subroutine's first argument
R5 + 4
Top of the stack is held in
R6
What to save at start of function? Why do we save it? How to build stack? How to load arguments off stack?
REST OF OLD REGISTERS LOCAL (NEW R5) R5 R7 RV PARAMETERS
RET
Return Equivalent to JMP R7 PC = R7
Addressing modes: ST, STI, STR
ST: pc-relative STI: indirect STR: base+offset
.orig
Tells the assembler to put this block of code at the desired address given
Who pops the return value and arguments off of the stack?
The caller!
.orig x3000 LD R0, HELLO LDR R1, R0, 1 HALT HELLO .fill x6000 .end .orig x6000 .stringz "Hi" .end What happens on line 2
The value located at the address of the label HELLO is loaded into SR0
.stringz
Will put a string of characters in memory followed by a null character (0)
Does HALT take up a memory address?
Yes
What does * after a type denote?
char *x, declares that x is a pointer to a char
char**y
declares that y is a pointer to a pointer to a char
When data gets pushed onto the stack, R6 is
decremented
.end
denotes the end of an address block, matches with .orig
the stack grows
down (into lower memory)
R7
holds the current return address
R5
holds the frame pointer
why do we need a frame pointer
if we undertsand the entire set of data a function pushes onto the stack as its stack frame, then we need to know where this data is stored and where relevant memory is
What do imm5, offset6, PCoffset9, PCoffset11, and trapvect8 mean? Understand the range of values for each of the immediate/offset sizes, and the implications of these limitations.
imm5: 15 to -16 ;; offset6: 31 to -32 ;; offset9: 255 to -256 ;; offset11: 1023 to -1024 ;; trapvect8:: does it matter?
When data is popped of the stack, R6 gets
increments
.orig x3000 LEA R0, BESTCS PUTS HALT BESTCS .stringz "CS2110" .end What is happening here?
line one will load the address represented by the BESTCS label into R0 line two will use use the starting address in R0 to print a string of chars until reaching the null characters
LD
loads data at a certain memory address into the designated register
LEA
loads the memory address into a designated register
Are labels stored anywhere
no
Does a pointer contain the type of data found at the address
no
does R5 change through a subroutine
no
does R5 change throughout a function's execution
no
Motivation for the stack
register saving and intelligent use of our 8 general purpose register is the main motivation for the stack
calling convention is a ____ abstraction
software
R6
stack pointer (top of the stack)
Halt
stops the LC3 from running x25
Traps are
subroutines built into the LC3 to help simplify instructions
GETC
takes in a character input and stores it in R0 x20
LDR
takes memory address from a register, adds it to an offset, retrieves the data at the offset + address memory address, and loads it into a register
.orig x3000 LD R0, HELLO LDR R1, R0, 1 HALT HELLO .fill x6000 .end .orig x6000 .stringz "Hi" .end What happens on line 3
takes the memory address x6000 located in R0 and adds 1 to give you x6001
Calling convention tells us
the standardized process for building up the stack when we want to execute a subroutine
The stack is a region in memory
to store temporary program data
Pointers
variables that contain a memory address
.blkw n
will allocate the next n locations of memory for a specified label
PUTS
will print a string of characters with the starting address saved in R0 until it reaches a 0 char x22
.fill value
will put that value in that memory location
OUT
will take whatever char is in R0 and print it x21
where is the trap vector table
x0000 to x00FF
Trap hex values
x20: GETC x21: OUT x22: PUTS x23: IN x25: HALT
.orig x3000 A .fill 2 ARRAY .fill x6000 .end
x3000: 2 x3001: x6000 The label never goes into memory! A simply points to this address
.orig x3000 string_label .stringz "Hey" block_label .blkw 2 fill_label .fill x1234 .end Draw this out in memory
x3000: H x3001: e x3002: y x3003: 0 x3004: blocked x3005: block x3006: x1234
function A calls function B. The instruction that tells the PC to jump to B's address is at x300B. Function B lives at x4000. What does R7 hold?
x300C (current return address)