You are on page 1of 18

COMP0068 Computer Architecture and Operating

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

• In the previous lecture we covered enough MIPS


instructions to write a very basic calculator that could
add three fixed numbers together and put the result
back in memory !

• In this lecture we will examine this calculator in more


detail and introduce a number of assembly language
features including:
– Assembly directives.
– MIPS memory layout.
– Assembly labels.

2
What’s the point of this lecture ?

By the end of this lecture you should know:

1. How to write real assembly language


programs.
2. How memory is laid out and utilized within
assembly language programs.
3. How to use MARS to simulate MIPS assembly
language programs - single stepping,
examining registers, etc.

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

• Assembly directives tell the assembler what


to do (they direct the assembly process).
• With MIPS assembly the directives all begin
with a full stop.
• Two key directives are ‘.data’ and ‘.text’.

• ‘.data’ and ‘.text’ are concerned with the way


the assembly language program is laid out
within memory.
5
0xFFFFFFFC
MIPS Memory RESERVED
Layout 0x80000000
0x7FFFFFFC
More about this STACK
later.

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 ?

This is a “data layout directive”

# Calculator Version 0.0


It allows the assembly
programmer to describe data in a
.data
more natural manner than binary!
vals: .word 4, 5, 8, 0

In this case the 32-bit words that


.text follow are written into the data
.globl main segment of the program.
main:
la $4, vals
add $8, $0, $0 # Set sum to zero
… etc.
9
Assemblers generally have lots of data
layout directives available …
.word 10,20,30 Store 10,20 and 30 in successive memory
locations as 32-bit words.
.half 10,20,30 Store 10,20 and 30 in successive memory
locations as half-words.
.byte 10,20,30 Store 10,20 and 30 in successive memory
locations as bytes.
.float 3.14, 1.99 Store 3.14 and 1.99 in successive memory
locations as floating point numbers (more
about this later).
.asciiz “Hello World\n” Store the string in successive memory
locations adding a zero byte to end.
.space 100 Allocate 100 bytes space in successive
memory locations.
10
Online Further Reading specifies even more data layout directives.
So we have an understanding of the first
segment of the assembly listing …

Within the data segment of the


# Calculator Version 0.0 program, write 16 bytes consisting
.data of 4 words with the values 4, 5, 8
vals: .word 4, 5, 8, 0 and 0.

.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 …

# Calculator Version 0.0


.data A colon after a word indicates an
“address label” – a symbolic
vals: .word 4, 5, 8, 0
representation of that address.
These can then be used within the
.text assembly code and the assembler will
.globl main put in the correct address values.
main:
la $4, vals # Load $4 with the address of label 'vals'.
add $8, $0, $0 # Set sum to zero
… etc.
12
Assembly Labels …

“main” is another address label.


In this case it labels the start of the
# Calculator Version 0.0 executable program – the ‘main’ function
of the program (as in the C language).
.data
vals: .word 4, 5, 8, 0 The additional directive ‘.globl’ tells the
assembly this is a ‘global symbol’ …
one that needs to be accessed outside
.text the current assembly program (so that
.globl main the operating system can run it).
main:
la $4, vals # Load $4 with the address of label 'vals'.
add $8, $0, $0 # Set sum to zero
… etc.
13
We can single step through the program
using MARS to see how it works …

• MARS has built in editor for MIPS assembly.


• MARS can then load the assembly program into
memory.
• It then lets you run the assembly program as if it was
being run by MIPS hardware.
• You can set breakpoints and single step to see exactly
how the registers and memory locations change.
• Let us work through the Calculator Version 0.0
program using MARS.
14
Getting the processor to work with data
other than 32-bit words
We have seen that lw and sw can load and store 32-bit words
from and to memory.

For other data types we have similar operations:


lh $1, 0x0000($2) # Load half-word.
lhu $1, 0x0000($2) # Load half-word unsigned.
sh $1, 0x0000($2) # Store half-word.

lb $1, 0x0000($2) # Load byte.


lbu $1, 0x0000($2) # Load byte unsigned.
sb $1, 0x0000($2) # Store byte.
15
5 minute Activity

• Form into pairs or groups of three.


• Discuss for 5 minutes what changes would be
required to get our calculator to work in bytes
rather than words. Hence it should read 3 bytes of
data, add them together and store the result in the
4th byte of memory.
• I’ll then randomly ask some groups and see if we
can get our assembly program to work with bytes.

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

• In this lecture we have used MARS and examined how our


calculator version 0.0 program works in detail.

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)

18

You might also like