How Computers Work Assembly Tools Calling Conventions: Processor State Instruction Memory Fetch/Execute Loop
How Computers Work Assembly Tools Calling Conventions: Processor State Instruction Memory Fetch/Execute Loop
How Computers Work Assembly Tools Calling Conventions: Processor State Instruction Memory Fetch/Execute Loop
Lecture 2
Assembly Tools
Calling Conventions
Review: Model of
Computation Fetch/Execute
Processor State Instruction Memory Loop:
PC Fetch <PC>
PC <pc> + 1
r0 Execute fetched
instruction
r1 32 bits
r2 (4 bytes) Repeat!
32 bits
next instr
r31 always 0
1
Review: BETA Instructions
Two 32-bit Instruction Formats:
OPCODE Ra Unused Rb Rc
2
Review: Loads & Stores
LD(ra, C, rc) rc < Mem[<ra> + sext(C)] >
Review: Branches
Conditional: rc = <PC>+1; then
BRNZ(ra, label, rc) if <ra> nonzero then
PC <- <PC> + displacement
BRZ(ra, label, rc) if <ra> zero then
PC <- <PC> + displacement
3
Review: Iterative Optimized Factorial
;assume n = 20, val = 1 n: 20
val: 1
Language Tools
The Assembler
01101101
11000110 STREAM of Words
Assembler 00101111
to be loaded
10110001
.....
into memory
Symbol
Textual Memory +
Macro Expression
Pre-Processor Evaluator
4
Macros
Macros are parameterized abbreviations that when invoked cause
TEXTUAL SUBSTITION
Is translated into:
5
Constant Expression Evaluation
37 -3 255 decimal (default);
0b100101
binary (0b prefix);
0x25
hexadecimal (0x prefix);
Symbolic Memory
We can define SYMBOLS:
x = 1 | 1
y = x + 1 | 2
6
How Are Symbols Different
Than Macros?
Answer:
A macros value at any point in a file is the last
previous value it was assigned.
Macro evaluation is purely textual substitution.
A symbols value throughout a file is the very last
value it is assigned in the file.
Repercussion: we can make forward references to symbols
not yet defined.
Implementation: the assembler must first look at the entire
input file to define all symbols, then make another pass
substituting in the symbol values into expressions.
7
Address Tags
x: is an abbreviation for x =. -- leading to programs like
x: 0
by the definitions
.macro BR(lab, rc) BRZ (r31,lab, rc)
8
OK, How about recursive fact?
(define (fact n)
(if (= n 0)
Suppose caller:
1
1. Stores nin agreed-on
(* n (fact (- n 1))) location (say, r1)
) )
2. Calls fact using, say,
int fact(int n) BR(fact, r28)
{ Then fact:
if (n == 0)
1. Computes value, leaves
return (1); (say) in r0.
else
2. Returns via
return (n * fact(n-1));
} JMP(r28, r31)
OOPS!
We need a STACK ! ! !
9
fact(3) ...
Recursion...
Caller
n=3
fact(3)
n=2
fact(2)
n=1
fact(1)
n=0
fact(0)
Stack Implementation
one of several possible implementations
Lower addresses
Old Stuff
Conventions:
Builds UP on push (stacked data)
Memory: (stacked data)
SP points to first (stacked data)
UNUSED location. (stacked data)
<sp>
10
Support Macros for Stacks
sp = r29
push(rx) - pushes 32-bit value onto stack.
ADDC(sp, 1, sp)
ST(rx, -1, sp)
The Stack
STACK as central storage-management
mechanism...
argi (arg)
SIMPLE, EFFICIENT to implement using rtn PC
rtn
contiguous memory and a "stack pointer" locj (Local)
ORIGINAL USE: subroutine return points - sp
push/pop STACK DISCIPLINE follows
natural order of call/return nesting.
EXPANDED USE: "automatic" or STACK DISCIPLINE:
"dynamic" allocation of local variables. most recently
REVOLUTIONARY IMPACT: allocated location
is next location to
ALL modern machines, to varying extents;
be deallocated.
ALL modern languages
IMPACT:
BLOCK
STRUCTURE.
How Computers Work Lecture 2 Page 22
11
Call / Return Linkage
lp = r28
sp = r29
Using these macros and r28 as a linkage pointer, we can call f by:
BR(f, lp)
12
A Generic Stack Frame Structure:
The 6.004 Stack Discipline
RESERVED REGISTERS:
bp = r27. Base ptr, points to 1st local.
lp = r28. Linkage pointer, saved <PC>.
sp = r29. Stack ptr, points to 1st unused word.
xp = r30. Exception pointer, saved <PC>
STACK FRAME
(a.k.a. Function Activation Record):
arguments P
U
old <lp>
old <bp>
S
bp: H
Local variables
...
Temporaries
sp:
(as yet unused)
Entry Sequence:
f: PUSH(lp) | Save <LP>, <BP>
PUSH(bp) | for new calls.
MOV(sp,bp) | set bp=frame base
ALLOCATE(locals) | allocate locals
(push other regs) | preserve regs used
...
Return Sequence:
(pop other regs) | restore regs
MOV(val, r0) | set return value
MOV(bp,sp) | strip locals, etc
POP(bp) | restore linkage
POP(lp) | (the return <PC>)
JMP(lp) | return.
How Computers Work Lecture 2 Page 26
13
Stack Frame Detail
14
Procedure Linkage: The Contract
Recursive factorial
with stack-passed arguments
| (define (fact n)
| (if (= n 0) 1 (* n (fact (- n 1))))
| )
LD(bp,-3,r1) | r0 n
BRNZ(r1, big) | if n>0
MOVC(1,r0) | else return 1;
BR(rtn)
15
What did we Learn Today?
How to call functions
How to do recursive factorial
The 6.004 Stack Discipline
How to retrieve arguments and local variables
Next In Section
Practice with Stack Discipline
C Tutorial
http://www.csc.lsu.edu/tutorial/ten-commandments/bwk-tutor.html
16