OCW - Unit 3. Assembly Programming PDF
OCW - Unit 3. Assembly Programming PDF
Departamento de Informática
Grupo de Arquitectura de Computadores, Comunicaciones y Sistemas
UNIVERSIDAD CARLOS III DE MADRID
Félix García Carballeira, Alejandro Calderón Mateos, José Daniel García Sánchez
Contents
Easy architecture.
Easy to learn
4
ARCOS Computer Structure 4
What is a computer?
Data results
Processor
Instructions
001 AB 00000000101
Operation code
Registers
Operands Memory address
Numbers
Fetch PC 000100
MAR PC IR
Read
MBR Memory
PC PC + 1 MAR MBR
RI MBR
000100 0010000000000000
Execution
Jump to fetch
Memory
temp = v[k];
00001001110001101010111101011000
10101111010110000000100111000110 v[k] = v[k+1];
11000110101011110101100000001001
v[k+1] = temp;
01011000000010011100011010101111
temp = v[k];
High level language v[k] = v[k+1];
(ej: C, C++) v[k+1] = temp;
compiler
lw $t0, 0($2)
Assembly language lw $t1, 4($2)
sw $t1, 0($2)
(Ej: MIPS) sw $t0, 4($2)
assembler
Machine language 0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
(MIPS) 1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
MIPS R2000/R3000
32 bits processor
RISC
CPU +
auxiliary coprocessors
Coprocessor 0
exceptions, interrupts and
virtual memory system
Coprocessor 1
FPU (floatingg point unit)
Transfer instruction
Arithmetic
Logical instructions
Shifting
Rotation
Comparison
Branches
Register to register
move $a0 $t0 $a0 $t0
int a = 5; li $t0, 5
int b = 7; li $t1, 7
int c = 8; li $t2, 8
int d;
int a = 5; li $t0, 5
int b = 7; li $t1, 7
int c = 8; li $t2, 8
int d; li $t3 10
d=-(a*(b-10)+c)
sub $t4, $t1, $t3
mul $t4, $t4, $t0
add $t4, $t4, $t2
li $t5, -1
mul $t4, $t4, $t5
Boolean operations
Examples:
AND
1100
and $t0 $t1 $t2 ($t0 = $t1 & $t2)
1010
AND
OR 1000
NOT NOT 10
not $t0 $t1 ($t0 = ! $t1) 01
XOR 1100
1010
or $t0 $t1 $t2 ($t0 = $t1 ^ $t2) XOR
0110
li $t0, 5
li $t1, 8
What is the value of $t2?
and $t2, $t1, $t0
li $t0, 5
li $t1, 8
What is the value of $t2?
and $t2, $t1, $t0
000 …. 0101 $t0
000 ….. 1000 $t1
and 000 ….. 0000 $t2
Bits movement
Examples:
Shift right logical 0
srl $t0 $t0 4 ($t0 = $t0 >> 4 bits) 01110110101
li $t0, 5
li $t1, 6
What is the value of $t0?
sra $t0, $t1, 1
li $t0, 5
li $t1, 6
Waht is the value of $t0?
sra $t0, $t1, 1
000 …. 0110 $t1
shift one bit to right
000 ….. 0011 $t0
li $t0, 5
li $t1, 6
What is the value of $t0?
sra $t0, $t1, 1
li $t0, 5
li $t1, 6
What is the value of $t0?
sra $t0, $t1, 1
000 …. 0110 $t1
Shit one bit to left
000 ….. 1100 $t0
Bits movement
Example:
Rotate left
rol $t0 $t0 4 rotate 4 bits
01110110101
Rotate right
ror $t0 $t0 5 rotate 5 bits
01110110101
int i;
li $t0 0
li $t1 10
i=0; while: bge $t0 t1 end
while (i < 10)
# action
{
/* action*/ addi $t0 $t0 1
i = i + 1 ; b while
} end: ...
}
Calculate 1 + 2 + 3 + …. + 10
i=0;
s=0;
while (i < 10)
{
s = s + i;
i = i + 1;
}
}
Result in $t1
Calculate 1 + 2 + 3 + …. + 10
i=0;
li $t0 0
s=0;
li $t1 0
while (i < 10)
li $t2 10
{
while: bge $t0 t2 end
s = s + i;
add $t1 $t1 $t0
i = i + 1;
addi $t0 $t0 1
}
b while
}
end: ...
Result in $t1
i = 0;
n = 45; #number
s=0;
while (i < 32)
{
b = last bit of n
s = s + b;
sift n one bit to
right
i = i + 1 ;
}
}
ARCOS Computer Structure 36
Solution
i = 0;
li $t0 0 #i
n = 45; #number
li $t1 45 #n
s=0;
li $t2 32
while (i < 32)
li $t3 0 #s
{
while: bge $t0 t2 end
b = last bit of n
and $t4 $t1 1
s = s + b;
add $t3 $t3 $t4
sift n one bit to
srl $t1 $t1 1
right
addi $t0 $t0 1
i = i + 1 ;
b while
}
end: ...
}
ARCOS Computer Structure 37
Example
Obtain the 16 first bits of a register ($t0) and store them in the
16 last bits of other register ($t1)
Obtain the 16 first bits of a register ($t0) and store them in the
16 last bits of other register ($t1)
0
01110110101 Shift 16 bits to right
int b1 = 4;
int b2 = 2; li $t0 4
li $t1 2
li $t2 8
if (b2 == 8) {
b1 = 0;
} bneq $t0 $t2 end
... li $t0 0
end: ...
end: ...
li $t2 9
li $t1 2
rem $t1 $t2 $t1 # remainder
beq $t1 $0 then # cond.
else: li $t1 0
b end # uncond.
then: li $t1 1
end: ...
a=8
n=4;
i=0;
p = 1;
while (i < n)
{
p = p * a
i = i + 1 ;
}
}
a=8 li $t0 8
n=4; li $t1 4
i=0; li $t2 1
p = 1; li $t4 0
while (i < n)
{ while: bge $t4 $t1 end
p = p * a mul $t2 $t2 $t0
i = i + 1 ; addi $t4 $t4 1
} b while
} end: move $t2 $t4
http://pages.cs.wisc.edu/~larus/spim.html
SPIM is a simulator of the
MIPS architecture
Multiplatform:
Linux
Windows
MacOS
http://sourceforge.net/projects/spimsimulator/files/
0 32 registers 16
1 4 bytes of size (one 17
2 18
word )
3 19
Use $
4 20
5 21
6 22
7 23
8 24
9 25
10 26
11 27
12 28
13 29
14 30
15 31
0 $zero 16
1 17
2 18
3 19
4 20
5 Constant zero 21
6 Cannot be changed 22
7 23
8 24
9 25
10 26
11 27
12 28
13 29
14 30
15 31
0 $zero 16
1 17
2 Temprorary registers 18
3 19
4 20
5 21
6 22
7 23
8 $t0 $t8 24
9 $t1 $t9 25
10 $t2 26
11 $t3 27
12 $t4 28
13 $t5 29
14 $t6 30
15 $t7 31
0 $zero $s0 16
1 $s1 17
2 Preserved values $s2 18
3 $s3 19
4 $s4 20
5 $s5 21
6 $s6 22
7 $s7 23
8 $t0 $t8 24
9 $t1 $t9 25
10 $t2 26
11 $t3 27
12 $t4 28
13 $t5 29
14 $t6 30
15 $t7 31
0 $zero $s0 16
1 $s1 17
2 $v0 $s2 18
3 $v1 $s3 19
4 $a0 $s4 20
5 $a1 $s5 21
6 $a2 $s6 22
7 $a3 $s7 23
8 $t0 $t8 24
9 $t1 $t9 25
10 $t2 26
11 $t3 27
12 $t4 Arguments and 28
13 $t5 subroutines $sp 29
14 $t6 $fp 30
15 $t7 $ra 31
0 $zero $s0 16
1 $at $s1 17
2 $v0 $s2 18
3 $v1 $s3 19
4 $a0 $s4 20
5 $a1 $s5 21
6 $a2 $s6 22
7 $a3 $s7 23
8 $t0 $t8 24
9 $t1 $t9 25
10 $t2 $k0 26
11 $t3 $k1 27
12 $t4 Others $gp 28
13 $t5 $sp 29
14 $t6 $fp 30
15 $t7 $ra 31
4GB of memory
Data segment
pc
Text segment 01011001
Assembly directives
Data section
.data
# Global static definitions
.text
# instructions
Code section
Code
segment
Data
segment
Stack
segment
Others
Code
segment
Data
segent
Stack
segment
Hello.s
.data
msg_hola: .asciiz "Hello world\n"
.text
.globl main
main:
# printf(" Hello world\n") ;
li $v0 4
la $a0 msg_hola
syscall
# coments
Instruction/pseudoinstructino
label: operands
.directive
Hello.s
.data
msg_hola: .asciiz "Hello world\n"
.text
.globl main
main:
# printf(" Hello world\n") ;
li $v0 4
la $a0 msg_hola
syscall
Hello.s
.data
msg_hola: .asciiz "Hello world\n"
.text
.globl main
main:
# printf("Hello world\n") ;
li $v0 4 #system call code
la $a0 msg_hola
syscall
Directives Description
.data Data segment definition
.text Code segment definition
.ascii “string” String definition without NULL terminator
.asciiz “string” String definition with NULL terminator
.byte 1, 2, 3 Definition of bytes in memory
.half 300, 301, 302 Definition of half-words
.word 800000, 800001 Definition of words
.float 1.23, 2.13 Definition of float values
.double 3.0e21 Definition of double values
.space 10 Reserve 10 bytes
.extern label n Label is global and extern of size n
.globl label Label is global
.align n Align next data to 2^n
How to invoke:
Call code in $v0.
Arguments in registers.
Execute syscall instruction
2n-1
Address: 0x0F000002
0x0F000000
0x0F000001 Content: 00001101 (9)
0x0F000002 00001101
0x0F000003
2n-1
2n-4
2n-1
$t1
31 24 23 16 15 87 0
2n-1
2n-1
$t1
31 24 23 16 15 87 0
2n-1
2n-1
2n-1
$t1
31 24 23 16 15 87 0
2n-1
2n-1
2n-1
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
00000…….000101
2
5 3 2 7
$t1 0 0 0 3
31 24 23 16 15 87 0
Byte Byte
addresses (content) A word: four bytes
0
1
2 Word stored in byte 0
3
4
5
6 Word stored in byte 4
7
8
2n-1
Byte Byte
addresses (content)
0
1
2
3
4
5
? byte0
32-bit word
byte1 byte2 byte3
6 31 24 23 16 15 87 0
7 + significant - significant
8
2n-1
32-bit word
byte0 byte1 byte2 byte3
31 24 23 16 15 87 0
+ significant - significant
A byte0 A byte3
A+1 byte1 A+1 byte2
A+2 byte2 A+2 byte1
A+3 byte3 A+3 byte0
BigEndian LittleEndian
A 00000000 A 00011011
A+1 00000000 A+1 00000000
A+2 00000000 A+2 00000000
A+3 00011011 A+3 00000000
BigEndian LittleEndian
A 00000000 A
A+1 00000000 A+1
A+2 00000000 A+2
A+3 00011011 A+3
BigEndian LittleEndian
A 00000000 A
A+1 00000000 A+1
A+2 00000000 A+2
A+3 00011011 A+3
Network transfer
BigEndian LittleEndian
A 00000000 A 00000000
A+1 00000000 A+1 00000000
A+2 00000000 A+2 00000000
A+3 00011011 A+3 00011011
Network transfer
BigEndian LittleEndian
A 00000000 A 00000000
A+1 00000000 A+1 00000000
A+2 00000000 A+2 00000000
A+3 00011011 A+3 00011011
BigEndian LittleEndian
Byte
0
1
2
3 Address: 4 (000110)
4 00000000
5 00000000
Content : 00000000000000000000000100001101(2 = 269(10
6 00000001 BigEndian
7 00001101
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
0x08
$t1 00000000 00000000 00000000 00001000
31 24 23 16 15 87 0
Byte Byte
addresses (content) li $t1, 18
li $t2, 24
0x0F000000
0x0F000001
0x0F000002
0x0F000003
0x0F000004
0x0F000005
0x0F000006
0x0F000007
Byte Byte
addresses (content) li $t1, 18
li $t2, 24
0x0F000000
0x0F000001
0x0F000002
0x0F000003
0x0F000004
0x0F000005
0x0F000006
0x0F000007
$t1 00000000 00000000 00000000 00010010
31 24 23 16 15 87 0
Byte Byte
addresses (content) sw $t1, 0x0F000000
Write the content of a register (word ) in memory
0x0F000000 00000000
0x0F000001 00000000
0x0F000002 00000000
0x0F000003 00010000
0x0F000004
0x0F000005
0x0F000006
0x0F000007
$t1 00000000 00000000 00000000 00010010
31 24 23 16 15 87 0
Byte Byte
addresses (content) sw $t1, 0x0F000000
sw $t2, 0x0F000004
0x0F000000 00000000
0x0F000001 00000000
0x0F000002 00000000
0x0F000003 00010000
0x0F000004 00000000
0x0F000005 00000000
0x0F000006 00000000
0x0F000007 00011000
$t1 00000000 00000000 00000000 00010010
31 24 23 16 15 87 0
Byte Byte
addresses (content) sb $t1, 0x0F000001
Write the less significant byte of register $t1 in
memory
0x0F000000
0x0F000001 00010010
0x0F000002
0x0F000003
0x0F000004
0x0F000005
0x0F000006
0x0F000007
$t1 00000000 00000000 00000000 00010010
31 24 23 16 15 87 0
Byte Byte
addresses (content) lw $t3, 0x0F000000
0x0F000000 00000000
0x0F000001 00000000
0x0F000002 00000000
0x0F000003 00010000
0x0F000004 00000000
0x0F000005 00000000
0x0F000006 00000000
0x0F000007 00011000 $t3 00000000 00000000 00000000 00010010
31 24 23 16 15 87 0
Address
31 23 15 7 0
0 This word is aligned.
4
8
12
16
20
24
Many computers does not allow the access to not aligned data:
Goal: reduce the number of memory accesses
Compilers assign addresses aligned to variables
Intel architectures allow the access to not aligned data
We need several memory accesses
32-bit word
0
1 00000000 00000000 00001110
2 00001101
3
4 byte 0 byte 1 byte 2 byte 3
Is translated to:
add reg2,$zero,reg1
lw
sw
lb Register, memory address
sb
lbu Number that represent sthe address
Symbolic label that represents the
adress
(register): address stored in a
register
num(register): represent sthe
address that is obtained adding
num and the address stored in the
register
Byte Byte
addresses (content)
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
$t0
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
Byte Byte
addresses (content) la $t0, 0x0F000002
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
$t0
31 24 23 16 15 87 0
$t1
31 24 23 16 15 87 0
Byte Byte
addresses (content) la $t0, 0x0F000002
$t1
31 24 23 16 15 87 0
Byte Byte
addresses (content) lbu $t0, ($t1)
0x0F000000 0xCB
0x0F000001 0x12
0x0F000002 0x08
0x0F000003 0x02
$t1
31 24 23 16 15 87 0
Byte Byte
addresses (content) lbu $t0, ($t1)
Byte Byte
addresses (content) lw $t0, ($t1)
sw $t0, 0x0F000000
Copy the word stored in $t0 in the address 0x0F000000
sb $t0, 0x0F000000
Copy the byte stored in $t0 in the address 0x0F000000
main () .text
{ .globl main
vec[4] = 8;
main: la $t1 vec
} li $t2 8
sw $t2 16($t1)
...
main () .text
{ .globl main
vec[4] = 8;
main: li $t0 16
} la $t1 vec
add $t3, $t1, $t0
li $t2 8
sw $t2, ($t3)
...
char c1 ; .text
char c2=‘h’ ; .globl main
char *ac1 = “hola” ;
... main: li $v0 4
la $a0 ac1
syscall
main ()
...
{
printf(“%s”,ac1) ;
...
}
Review:
lw $t0, 4($s3) # $t0 M[$s3+4]
sw $t0, 4($s3) # M[$s3+4] $t0
int vec[100] ;
...
main ()
{
int i = 0;
int vec[100] ;
...
main ()
{
int i = 0;
suma = 0;
$a0 stores the init address of v. The result must be stored in $v0
ALU
BUS
f0 = f1 + f4
ALU
Addition in double precision
add.d $f0 $f2 $f4 BUS
U.C
ALU ALU
BUS BUS
U.C
ALU ALU
BUS BUS
ALU ALU
BUS BUS
$f0 ← $f1
ALU ALU
BUS BUS
BUS BUS
jr $ra
Operands
Location of operands
Location for results
Location of next instruction (in branches)
Implicit : PC PC + ‘4’ (next instruction)
Explicit: j 0x01004 (PC modified)
In the instruction
In registers (processor)
Main memory
Input/output modules
Example:
li $a0 25
addi $t1 $t2 50
Operand is in a register.
Example: move $a0 $a1
$a0 and $a1 are encoded in the instruction
op rs rt 16 bits
operand
Advantages: Regs.
No memory access is required
Small address field
Faster access
Operand
op rs rt 16 bits
Problems:
Memory access time is larger than register access time
Large fields=> large instructions
op rs rt 16 bits
Advantages:
Small fields
MIPS can allow in a register an address for addressing the entire memory
(registers have 32 bits)
op address 1
Problems:
Several memory accesses are required
Slower instructions
Instruction
Op. cod. Register R Displacement
Memory
Registers bank
.data
.align 2 #next item align to 4
int vec[5] ;
vec: .space 20 #5 elem.*4 bytes
...
.text
main ()
.globl main
{
v[4] = 8; main: la $t1 vec
li $t2 8
sw $t2 16($t1)
} ...
Instruction
op. cod. Register R address
Memory
Registers
Index/displacement + Operand
.data
.align 2 # next item align to 4
int vec[5] ;
vec: .space 20 #5 elem.*4 bytes
...
.text
main ()
.globl main
{
v[3] = 8; main: li $t1 12
v[4] = 8; li $t2 8
sw $t2 vec($t1)
addi $t1 $t1 4
} sw $t2 vec($t1)
...
Instruction
Op. code Displacement
PC +
Registers of 32 bits
Program counter of 32 bits
Instructions of 32 bits (one word)
Program counter stores instruction address
Next instruction is 4 bytes beyond
Program counter is updated:
PC = PC + 4
address: Instruction:
0x00400000 or $2,$0,$0
0x00400004 slt $8,$0,$5
0x00400008 beq $8,$0,3
0x0040000c add $2,$2,$4
0x00400010 addi $5,$5,-1
0x00400014 j 0x100001
6 5 5 16
CO rs rt immediate
end== 3
When an instruction is executed, PC stores the addresss of next
instruction in memory
Instruction j address
op. address
6 bits 26 bits
Instruction b displacement
op. displacement
6 bits 5 bits 5 bits 16 bits
op rs 16 bits
Advantages
Fast: No access memory is required.
Instructions shorter
item $sp
top $sp top
Immediate value
Register $r
Direct dir
Register indirect ($r)
Register relative displacement($r)
PC-relative beq label
Stack-relative displacement($sp)
Alternatives:
Unique length: All instructions with the same size
MIPS32: 32 bits
PowerPC: 32 bits
Variable: Different instructions can have different sizes
How to know the length of an instruction? Op. code
IA32 (Intel processors): variable number of bytes
6 5 5 5 5 6
CO rs rt rd sa func
CO rs rt immediate
MIPS Instruction:
add $8,$9,$10
Format:
6 5 5 5 5 6
CO rs rt rd sa func
Decimal representation:
0 9 10 8 0 32
Binary representation
MIPS Instruction:
addi $21,$22,-50
Format:
6 5 5 16
CO rs rt immediate
Decimal representation:
8 22 21 -50
Binary representation
001000 10110 10101 1111111111001110
How does the unit control know the format of each operand?
Fixed size:
n bits 2n operation codes
m operation codes log2m bits.
Extension fields
MIPS (arithmetic-logic instructions)
Op = 0; The instruction is encoded in func
Tipo R op. rs rt rd shamt func.
aritméticas
Variable sizes:
More frequent instructions= shorter sizes
Word of 16 bits
16 bits
16 bits
6 bits
Operation
code
16 bits
6 bits 3 bits 3 bits 3 bits
16 bits
6 bits 3 bits 3 bits 3 bits 1 bit
0 addresses Stack.
1 address Accumulator register.
2 addresses Registers, Register-memory, Memory-
memory
3 addresses registers, register-memory, memory-
memory.
Registers
Three operands in registers.
Require load/store operations.
ADD .R0, .R1, .R2
Memory-memory
Operands in memory addresses
ADD /DIR1, /DIR2, /DIR3
Register-memory
Hybrid.
ADD .R0, /DIR1, /DIR2
ADD .R0, .R1, /DIR1
X = A + B * C
/DA A
/DB B
/DC C
Execution model
/DX X
0 accesses to registers
Registers:
Two operands in registers
Load/store operations requiered
ADD .R0, .R1 (R0 <- R0 + R1)
Memory-memory
Two operands in memory
ADD /DIR1, /DIR2 (MP[DIR1] <- MP[DIR1] +
MP[DIR2])
Register-memory
Hybrid.
ADD .R0, /DIR1 (R0 <- R0 + MP[DIR1])
0 accesses to registers
4 accesses to registers
0 access to registers
PUSH 5
PUSH 5
PUSH 7
7
5
PUSH 5
PUSH 7
ADD
12
PUSH 5
PUSH 7
ADD
POP /DX
int main() {
int factorial(int x) {
int z;
int i;
z=factorial(x);
int r=1;
print_int(z);
for (i=1;i<=x;i++) {
}
r*=i;
}
return r;
}
factorial:
… jr $ra
jal factorial
…
Factorial represents the init
address of the subroutine (function)
0x00401000 factorial:
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
$ra = PC = 0x00001004
PC = 0x00401000
0x00401000
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
$ra = 0x00001004
0x00401000
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
PC = $ra = 0x00001004
$ra = 0x00001004
0x00401000
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
PC = $ra = 0x00001004
0x00401000
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
jr $ra
$ra = PC = 0x00001004
PC = 0x00401000
0x00401000
0x00008000
0x00401020 jal 0x000080000
0x00401024
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
jr $ra
$ra = PC = 0x00401024
PC = 0x00008000
0x00401000
0x00008000
0x00401020 jal 0x000080000
0x00401024
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
jr $ra
PC = $ra = 0x00401024
0x00401000
0x00008000
0x00401020 jal 0x000080000
0x00401024
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
jr $ra
?
PC = $ra = 0x00401024
0x00401000
0x00008000
0x00401020 jal 0x000080000
0x00401024
…
0x00001000 jal 0x00401000 jr $ra
0x00001004 …
jr $ra
?
PC = $ra = 0x00401024
Main
Memory
Operating Disk
System
Code
Data
Program
Executable
Stack
File
memory
User programs are divided in
pc segments:
Text segment 01011001 Stack segment
Local variables
Data segment Function context
Data segment
Static data, global variables
$sp Text segment (code)
01011001 Machine instructions
Stack segment
item $sp
top $sp top
7 $sp
8
Initial state: stack pointer ($sp) points tto he last element in stack
$sp
7
8
9 $sp
7
8
li $t2, 9
sw $t2 ($sp)
Insert the content of register $t2 in the stack
9 $sp
7
8
lw $t2 ($sp)
Copy in $t2 the first element of the stack (9)
9
7
$sp
8
push $a0
push $t1
push $t2
push $s2
...
pop $s2
pop $t2
pop $t1
pop $a0
...
lw $s2 ($sp)
pop $s2
add $sp $sp 4
pop $t2
lw $s2 ($sp)
pop $t1
add $sp $sp 4
pop $a0
lw $s2 ($sp)
add $sp $sp 4
lw $s2 ($sp)
add $sp $sp 4
... ...
lw $s2 ($sp)
pop $s2
lw $t2 4($sp)
pop $t2
pop $t1 lw $t1 8($sp)
pop $a0 lw $a0 12($sp)
add $sp $sp 16
int main() {
int factorial(int x) {
int z;
int i;
z=factorial(5);
int r=1;
print_int(z);
for (i=1;i<=x;i++) {
.
r*=i;
.
}
.
return r;
}
}
In z=factorial(5);
Input parameter in $a0
Result in $v0
s = c * c * c;
return s;
}
int f2(int c)
{
int s;
s = c * c * c;
return s;
}
s = c * c * c;
return s;
}
r = a + a + f2(b);
return r; f2: mul $t0, $a0, $a0
} mul $t0, $t0, $a0
jr $ra
int f2(int c)
{
int s;
Function f2 does not modify register $ra because is terminal
Register $t0 is not stored in stack because this type of
s = c * c * c; register is temporary and its value does not need be
return s; preserved
}
Terminal function.
Does not call other functions.
Registers saved by
Arguments of
the calle
High address
jal function
lw $t1, 4($sp)
lw $t0, ($sp)
addu $sp, $sp, 8
$a0 Argument 1
$a1 Argument 2
$a2 Argument 3
Registers $t $a3 Argument 4
stack
li $a0, 5 // param 1
li $a1, 8 // param 2
jal func
$a0 Argument 1
$a1 Argument 2
$a2 Argument 3
Argument 5 $a3 Argument 4
Argument 6
Registers $t
li $a0, 5 // param 1
li $a1, 8 // param 2
li $a2, 7 // param 3
li $a3, 9 // param 4
jal func
Callee subroutine:
Restore the saved values
Low addresses
Before the call
$sp
High addresses
$sp
$fp $ra
$fp (old)
High addresses
$fp $ra
b_else: sw $a0 -8($fp)
addi $a0 $a0 -1 $fp (old)
jal factorial
lw $v1 -8($fp)
mul $v0 $v0 $v1
High addresses
$ra
b_else: sw $a0 -8($fp)
addi $a0 $a0 -1 $sp $fp (antiguo)
jal factorial
lw $v1 -8($fp)
mul $v0 $v0 $v1
# end frame stack
b_efs: lw $ra 4($sp)
lw $fp ($fp) High addresses
addu $sp $sp 12
jr $ra
ARCOS Computer Structure 289
O32 convention (MIPS)
Registers bank
Registers $t
pila
Compiled code:
Programs are translated to machine code by the compiler
Code directly executed by the computer
More efficient
Interpreted code:
An interpreterr is a program that executes other programs
An interpreter executes a set of instruction machine independent.
Example: Java is translated to byte code that is execute by the
interpreter (Java Virtual Machine)
Generally easier to write interpreter. More portability
C program
compiler
Assembly program
Assembler
Linker
Loader
Memory
Main
memory
Operating Disk
System
Code
Data
Program
Executable
Stack
file
The library routines are not linked into the executable file and
are loaded when the program is loaded in memory
The program includes information to locate the libraries and to
resolve the external references during the execution
Advantages:
Smaller executables
Only the elements used are loaded
A change in the library does not affect the executable file. Is
not necessary to re compilate the code
#include <stdio.h>
int main (int argc, char *argv[])
{
int i, sum = 0;
for (i = 1; i <= 10; i++)
sum = sum + i + i;
.text end:
.align 2 la $a0, str
.globl main li $a1, $t1
main: jal printf
subu $sp,$sp,24 move $v0, $0
sw $ra, 20($sp) lw $ra, 20($sp)
sw $a0, 4($sp) lw $a0, 4($sp)
sw $a1, 8($sp) lw $a1, 8($sp)
addiu $sp,$sp,24
li $t0, 0 jr $ra
li $t1, 0 .data
loop: .align 0
bgt $t0, 10, end
add $t1, $t1, $t0 str:
addi $t0, $t0, 1 .asciiz "The sum
1 + ... +10 is
b loop %d\n "
ARCOS Computer Structure 307
Compilation
.text end:
.align 2 la $a0, str
.globl main li $a1, $t1
main: jal printf
subu $sp,$sp,24 move $v0, $0
sw $ra, 20($sp) lw $ra, 20($sp)
sw $a0, 4($sp) lw $a0, 4($sp)
sw $a1, 8($sp) lw $a1, 8($sp)
addiu $sp,$sp,24
li $t0, 0 jr $ra
li $t1, 0 .data 7 pseudo-
loop: .align 0
instructiones
bgt $t0, 10, end
add $t1, $t1, $t0 str:
addi $t0, $t0, 1 .asciiz "The sum
1 + ... +10 is
b end %d\n "
ARCOS Computer Structure 308
Compilation
Elimination of pseudoinstructions
.text end:
.align 2 lui $4, l.str
.globl main ori $4, $4, r.str
main: addu $4, $0, $9
addiu $29,$29,-24 jal printf
sw $31, 20($29) addu $2, $0, $0
sw $4, 4($29) lw $31, 20($29)
sw $5, 8($29) lw $4, 4($29)
ori $8, $0, 0 lw $5, 8($29)
ori $9, $0, 0 addiu $29,$29,24
loop: jr $31
slti $1, $8, 11
beq $1, $0, end
add $9, $9, $8
addi $8, $8, 1
bgez $0, loop
ARCOS Computer Structure 309
Compilation
Assign addresses
00 addiu $29,$29,-24 2c lui $4, l.str
04 sw $31, 20($29) 30 ori $4, $4, r.str
08 sw $4, 4($29) 34 addu $4, $0, $9
0c sw $5, 8($29) 38 jal printf
10 ori $8, $0, 0 3c addu $2, $0, $0
14 ori $9, $0, 0 40 lw $31, 20($29)
18 slti $1, $8, 11 44 lw $4, 4($29)
1c beq $1, $0, end 48 lw $5, 8($29)
20 add $9, $9, $8 4c addiu $29,$29,24
24 addi $8, $8, 1 50 jr $31
28 bgez $0, loop