Computer Architecture: Vnu - University Engineering Technology
Computer Architecture: Vnu - University Engineering Technology
ARCHITECTURE
CONTENTS
> Introduction
> If statements
> Loops
> Arrays and pointers
> Procedure calling
> Translation and startup
Introduction
Key concept
Any form of loop can be written in assembly with the help of
conditional branches and jumps.
Loops (cont.)
Address of A[] ➔ t0
Result ➔ t6 Comments
i ➔ t1
addi t6, zero, 0
addi t1, zero, 0
addi t2, zero, 40 # end point
loop: bge t1, t2, end
slli t3, t1, 2 # i * 4
add t4, t0, t3 # &A[i]
lw t5, 0(t4) # t5 A[i]
bne t5, zero, skip
addi t6, t6, 1 # result++
skip: addi t1, t1, 1 # i++
j loop
end:
Assembly Implementation for Pointer
Address of A[] ➔ t0
Result ➔ t6 Comments
&A[i] ➔ t1
addi t6, zero, 0
addi t1, t0, 0 # pointer to &A[current]
addi t2, t0, 160 # end point: &A[40]
loop: bge t1, t2, end # comparing address!
lw t3, 0(t1) # t3 A[i]
bne t3, zero, skip
addi t6, t6, 1 # result++
skip: addi t1, t1, 4 # move to next item
j loop
end:
> How does RV32I provide ISA-level support for HLL calls
▪ to leaf procedures?
▪ to nested procedures?
Implementing Procedure Call
Main program
Address
1000 Instruction 1
1004 Instruction 2 2. Control passes Procedure
1. Procedure 1008 Instruction 3 to the procedure
call 1012 Procedure call Address
3. procedure
1016 Instruction 𝐼𝑛𝑒𝑥𝑡
execution 2000 Instruction 1
1020 … 4. Control returns to 2004 Instruction 2
1024 … main program
High address
sp Saved s1 sp Saved s1
Saved s0 Saved s0
sp
Low address
Before call During call After call
Implementing Stack in RVI32I
> Key steps:
▪ Spilling: calculate the amount of space for spilling registers & decrease sp by the
amount of space we need, then fill it with data via store instructions.
▪ Restoring: restore the registers to previously spilled values via load instructions,
then increase sp by the same amount to clear the stack.
> Example:
Leaf: addi sp,sp,-8 # adjust stack for 2 items
sw s1, 4(sp) # save s1 for use afterwards
sw s0, 0(sp) # save s0 for use afterwards
add s0,a0,a1 # f = g + h
add s1,a2,a3 # s1 = i + j
sub a0,s0,s1 # return value (g + h) – (i + j)
sumSquare:
addi sp,sp,-8 # space on stack “push” non-
sw ra, 4(sp) # save ret addr preserved
sw a1, 0(sp) # save y
registers
mv a1,a0 # mult(x,x)
jal mult # call mult
lw a1, 0(sp) # restore y
add a0,a0,a1 # mult()+y “pop” non-
lw ra, 4(sp) # get ret addr
preserved
addi sp,sp,8 # restore stack
jr ra registers
mult: ...
Memory Layout
❑ Text: program code (binary instruction codes)
❑ Data:
➢ Static data: variables declared once per program, e.g. global variables
➢ Dynamic data: variables declared dynamically, e.g. heap (malloc in C)
➢ Stack: stores saved registers & variables are local to function & discarded
when function exits (called automatic variables) that don’t fit in registers.
▪ E.g. local arrays
sum.c
Many compilers produce
object modules directly
sum.s
sum.obj
Static linking
sum.exe
Compiler Interpreter
How it converts the input? Entire program at once Read one instruction at a time
When is it needed? Once, before the 1st run Every time the program is run
What ➢
it slows
Somedown
languagesProgram
mix bothdevelopment
concepts, e.g. Java. Program execution
Compiling
❑ Assembler (or compiler) translates HLL into machine language
using a (modified) Harvard architecture
➢ Has distinct code and data caches, backed by a common address space.
➢ Most assembler instructions represent machine instructions one-to-one
➢ Pseudo-instructions: figments of the assembler’s imagination, e.g.
mv t0, t1 → add t0, zero, t1
❑ Linking libraries
➢ Static linking: all/most needed routines in the library are loaded as part of
the executable code → image bloat & recompilation of the whole library
when new versions arise.
➢ Dynamically linked libraries (DLLs): only link/load library procedure when it
is called → adds complexity to the compiler, but avoids image bloat &
automatically pick up new library versions.
Example: Linking of two RV64 obj files
A.obj
Linking
Assembling B.obj
Loading
❑ Load from image file on disk into memory for execution
1. Reads executable file’s header to determine size of text and data segments
2. Creates new address space for program large enough to hold text and data
segments, along with a stack segment
3. Copies instructions + data from executable file into the new address space
4. Copies arguments passed to the program onto the stack
5. Initialize registers (most registers cleared, but sp assigned address of 1st
free stack location)
6. Jump to startup routine
▪ Copies program’s arguments from stack to registers & sets the PC
▪ When main returns, start-up routine terminates program with the exit
system call
> In class
▪ Reinforcement/enrichment discussion
▪ Quizzes