Professional Documents
Culture Documents
Systems
Lecture 7: MIPS Assembly Language
Further Reading
• MARS Web site at:
http://courses.missouristate.edu/KenVollmar/MARS/
• Appendix A of Patterson & Hennessey available free at:
http://www.cs.wisc.edu/~larus/HP_AppA.pdf
(also given on the Moodle site)
Overview of this Lecture
2
What’s the point of this lecture ?
3
Recall Calculator Version 0.0
# Calculator Version 0.0
.data
vals: .word 4, 5, 8, 0
.text
.globl main
main:
la $4, vals # Load $4 with 'vals‘ address.
add $8, $0, $0 # Set sum to zero
lw $9, 0($4) # Get first number from memory
add $8, $8, $9 # Add to sum
lw $9, 4($4) # Get second number from memory
add $8, $8, $9 # Add to sum
lw $9, 8($4) # Get third number from memory
add $8, $8, $9 # Add to sum
sw $8, 12($4) # Save the result to memory.
li $v0 10 # Exit program - system call 10.
4
syscall # More on this later ....
Assembly Directives
Data segment
stores global
data and also
dynamic data DYNAMIC DATA
0x10010000
0x1000FFFC
This is where GLOBAL DATA
the executable 0x10000000
machine code 0x0FFFFFFC
TEXT
for the program 0x00400000
is stored 0x003FFFFC
RESERVED 0x00000000
MIPS Memory Layout
• Loading calculator version 0.0 into MARS again, we see:
• Executable code is loaded at the memory address 0x00400000
For historic reasons, this is known as the ‘Text Segment’.
• Data is loaded at the memory address 0x10010000 upwards.
This is known as the ‘Dynamic Data Segment’.
DYNAMIC DATA
• When running 0x10010000
the program, the
GLOBAL DATA
default is to run it 0x10000000
from address
0x00400000. TEXT SEGMENT
0x00400000
7
Thus the ‘.data’ and ‘.text’ directives tell
the assembler which memory segments to
put the following assembly code into.
# Calculator Version 0.0
.data … INTO DATA
vals: .word 4, 5, 8, 0
SEGMENT
.text
.globl main … INTO TEXT
main:
SEGMENT
la $4, vals # Load $4 with the address of label 'vals'.
add $8, $0, $0 # Set sum to zero
… etc.
8
What about this ‘.word’ directive ?
.text
.globl main But what is this ?
main:
la $4, vals # Load $4 with the address of label 'vals'.
add $8, $0, $0 # Set sum to zero
… etc.
11
Assembly Labels …
We don’t actually have much idea at what address data or instructions
will be put in memory !
But didn’t you just say data will be put starting from 0x10010000 …
16
# Calculator Version 0.0
.data
vals: .word 4, 5, 8, 0
.text
.globl main
main:
la $4, vals # Load $4 with 'vals‘ address.
add $8, $0, $0 # Set sum to zero
lw $9, 0($4) # Get first number from memory
add $8, $8, $9 # Add to sum
lw $9, 4($4) # Get second number from memory
add $8, $8, $9 # Add to sum
lw $9, 8($4) # Get third number from memory
add $8, $8, $9 # Add to sum
sw $8, 12($4) # Save the result to memory.
li $v0 10 # Exit program - system call 10.
17
syscall # More on this later ....
Lecture Summary
Further Reading
18