Title: Appendix A
1Appendix A
2Appendix A
http//chortle.ccsu.edu/AssemblyTutorial/Chapter-0
9/ass09_1.html
3Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
call
return
Code with a function
Original code
4Appendix B - Jump-and-link
- Instructions to cope with functions in Assembly
M
e
m
o
r
y
o
p
A
d
d
r
e
s
s
4
W
o
r
d
4 most significant bits
P
C
4
call
31
return
Code with a function
Original code
5Appendix B - Jump-and-link
- Instructions to cope with functions in Assembly
jal
M
e
m
o
r
y
1
3
4
W
o
r
d
4 most significant bits
4
0
0
0
0
0
1
8
PC
4
4
31
funct1 add 2, 6, 7 main add 6, 13,
14 sub 7, 12, 18 jal funct1
addi 5, 19, 2
0x40000004 0x40000008 .... 0x40000010 0x40000
014 0x40000018 0c4000001c
New instruction jal label
6Appendix B - Jump-and-link
- Instructions to cope with functions in Assembly
jal
M
e
m
o
r
y
1
3
4
W
o
r
d
4 most significant bits
4
0
0
0
0
0
0
4
PC
4
31
0x40000004 0x40000008 .... 0x40000010 0x40000
014 0x40000018 0c4000001c
funct1 add 2, 6, 7 main add 6, 13,
14 sub 7, 12, 18 jal funct1
addi 5, 19, 2
New instruction jal label
7Appendix B - Jump-and-link
- Instructions to cope with functions in Assembly
M
e
m
o
r
y
o
p
A
d
d
r
e
s
s
4
W
o
r
d
4 most significant bits
P
C
4
call
31
return
0x40000004 0x40000008 .... 0x40000010 0x40000
014 0x40000018 0c4000001c
funct1 add 2, 6, 7 main add 6, 13,
14 sub 7, 12, 18 jal funct1
addi 5, 19, 2
Code with a function
Original code
8Appendix B - Jump-and-link
- Instructions to cope with functions in Assembly
o
p
r
s
r
t
r
d
.
.
.
f
u
n
c
t
M
e
m
o
r
y
31
W
o
r
d
31
0
.
.
.
8
0
0
call
return
0x40000004 0x40000008 0x4000000c 0x40000010 0x4000
0014 0x40000018 0c4000001c
funct1 add 2, 6, 7 ....
jr 31 main add 6, 13, 14 sub 7,
12, 18 jal funct1 addi 5, 19,
2
Code with a function
Original code
9Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
Original code
10Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
Original code
11Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
31
Original code
12Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
31
Original code
13Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
31
Original code
14Appendix B
- Instructions to cope with functions in Assembly
- Functions are small pieces of code frequently
utilized. - Suppose we have a code to perform arithmetic on
complex numbers the conversions between polar
and rectangular representations are frequently
used in the code. Thus, one function to convert
from polar to rectangular is desired, as well as
one from rectangular to polar.
Rec2pol add .... sub ....
jr 31 return Pol2rec mul ....
add.... jr 31
return main lui ....
lw.... jal Rec2pol call
add.... sw....
call
return
Code with a funtion
31
Original code
15Appendix B
- Functions may require in parameters (arguments)
and yield out results (values for results) - Procedures may require in parameters and do NOT
yield out results
16Policy of Use Conventions
Appendix B -Registers Policy of Use Convention
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29 30 31
zero reserved v0 v1 a0 a1 a2 a3 t0 t1 t2
t3 t4 t5 t6 t7 s0 s1 s2 s3 s4 s5 s6
s7 t8 t9 reserved reserved gp sp fp ra
Name
Register number
Usage
0
the constant value 0
zero
2-3
values for results
v0-v1
4-7
arguments
a0-a3
8-15
temporaries
t0-t7
16-23
saved
s0-s7
24-25
more temporaries
t8-t9
28
global pointer
gp
29
stack pointer
sp
30
frame pointer
fp
31
return address
ra
17Appendix B - Registers Policy of Use Convention
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29 30 31
zero reserved v0 v1 a0 a1 a2 a3 t0 t1 t2
t3 t4 t5 t6 t7 s0 s1 s2 s3 s4 s5 s6
s7 t8 t9 reserved reserved gp sp fp ra
Name
Register number
Usage
0
the constant value 0
zero
2-3
values for results
v0-v1
4-7
arguments
a0-a3
8-15
temporaries
t0-t7
16-23
saved
s0-s7
24-25
more temporaries
t8-t9
28
global pointer
gp
29
stack pointer
sp
30
frame pointer
fp
31
return address
ra
0x40000004 0x40000008 0x4000000c 0x40000010 0x4000
0014 0x40000018 0c4000001c
funct1 add 2, 6, 7 ....
jr 31 main add 6, 13, 14 sub 7,
12, 18 jal funct1 addi 5, 19,
2
18Appendix C - syscall
- Communicating with the World
Users Applications
OS
MACHINE (keyboard, display, ...)
19Appendix C - syscall
- Communicating with the World
Users Applications
OS
MACHINE (keyboard, display, ...)
syscall
20Appendix C - syscall
- Communicating with the World
Users Applications
- SPIM provides some simple I/O using the syscall
instruction. The specific I/O done depends on
some registers. - You set v0 to indicate the operation.
- Parameters in a0, a1
OS
MACHINE (keyboard, display, ...)
syscall
21Appendix C - syscall
- Communicating with the World
System call is used to communicate with the
system and do simple I/O. Load system call code
into Register v0 Load arguments (if any) into
registers a0, a1 or f12 (for floating
point). do syscall Results returned in registers
v0 or f0.
Users Applications
OS
MACHINE (keyboard, display, ...)
syscall
22Appendix C - syscall
addi v0,0, 5 Indicate we want function 5
syscall Upon return from the syscall, v0 has
the integer typed by a human in the SPIM
console Now print that same integer add a0,
0, v0 Get the number to be printed into
register addi v0,0,1 Indicate were doing
a write-integer syscall
- Communicating with
- the World
Users Applications
OS
MACHINE (keyboard, display, ...)
syscall