Lab 1 Notes
Lab 1 Notes
Introduction to MIPS/MARS
CPU: contains 32 user registers = storage for data within the CPU
The contents of these registers are used as operands in the program instructions. Although all these registers are
general purpose (can be used to store any kind of data), there are conventions for typical usage, as indicated.
There are also 7 other registers with specific purposes that you will soon learn more about.
0000 0000 0000 0000 0000 0000 0000 00002 , is equivalent to:
0 0 0 0 0 0 0 016
0010 0100 0011 0101 1000 1100 0001 11112 , is equivalent to:
2 4 3 5 8 C 1 F16
Memory:
There are 232 – 1 locations or addresses in memory in which a byte (8 bits) of information can be stored.
The locations can contain data or program instructions (although these are shown as mnemonics below, they are
actually stored as numeric values).
Each instruction in MIPS is 32 bits (one word) long.
lw $t1,label #load word, $t1 <- value of word stored at memory address/location specified by label
lw $t1,3($s0) #load word, $t1 <= value of word stored at memory address (base address in $s0 + 3)
sw $t1,label #store word, stores value of word in $t1 to address/location in memory specified by label
(lw and sw can also be byte or halfword, i.e. lb, lh, sb,sh)
Pseudo-instructions
Not part of the basic MIPS instruction set but used for programmer’s convenience. These instructions translate
to basic MIPS instructions when the program is assembled (but those basic instructions are not as intuitive from
a programmer’s perspective).
Directives – are not instructions, but you use them in your program to tell the assembler how to store your
program in memory
.text
.globl main
Precedes your text segment (program instructions), and specifies main as a global symbol (recognized
by other files in a multi-file project
.data
Precedes your data segment (data declarations)
(text segment can come before data segment, or vice versa)
.ascii “string”
Defines a string of characters (each character is stored as a 1-byte ascii value)
.asciiz “string”
Defines a null-terminated string (ends with a null byte)
.byte b0,b1,b2
Defines and initializes subsequent bytes in memory
.half h0,h1,h2
Defines and initializes subsequent half-words (16-bit
values – alignment forced to next even address
.word w0,w1,w2
Defines and initializes subsequent words (32-bit values)
– alignment forced to next word address (multiple of 4)
.space n
allocates n bytes of space, usually initialized to 0
#print an integer
li $v0,1 # load service number into $v0
li $a0,5 # load value to be printed into $a0
syscall
.data
prompt_string: .asciiz “Enter a value: “
#read in an integer
li $v0,5 #load service number in $v0
syscall #the value entered by the user is returned in $v0
move $t0,$v0 #store value entered into another register
#read in a string
li $v0,8 #load service number in $v0
la $a0,answer #put address of answer string in $a0
lw $a1,alength #put length of string in $a1
syscall #answer and alength must be defined in data segment!
.data
answer: .space 50 #allocate space for string to be stored
alength: .word 50 #length of string to be entered