CS241 Lecture Notes - Lecture 4: Hard Coding

81 views5 pages
Lecture 4
mult $s, $t
- meaning: multiply $s with $t
- where does the result go? The result can be larger than what can be stored in 32 bits so
there is no dest register.
- result is stored in special registers - hi:low (if result within 32 bits, stored in lo, otherwise
stored in hi).
- multu - unsigned multiplication
How to get the result after multiplying?
mfhi $d - move from hi into $d
mflo $d - move from lo into $d
div $s, $t
- meaning: divide $s by $t
- lo stores $s / $t (quotient)
- hi stores $s % $t (remainder)
- divu - unsigned division
ex.
$1 contains the address of a 32-bit array of integers
$2 contains the number of elements in this array
Note: this is a common start for a problem and then you'll be asked to do various things
Read array[3] into $3
(see picture)
$1 + (3 * 4)
lis $5
.word 3
lis $4
.word 4
mult $4, $5
mflo $5
add $5, $1, $5 // $5 <- 52 (from picture)
lw $3, 0($5)
jr $31
But we know the offset so we could also write...
lw $3, 12($1) // 12 is constant so we can do this
jr $31
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
mips.twoints isn't useful in these cases...
We have another simulator!
Simulator 2: mips.array
- Instead of asking to give values fo 2 registers, it will ask for the number of elements in
array and then ask you.
- for those elements
- $1 - start address of array
- $2 - number of elements in array
Procedures in Assembly
- Anything that you expect to do mulitple times, you write it in a procedure so you can call
it multiple times.
First problem:
In c, you can create local variables so code outside that function can't acce.ss them...
In assembly, you store values either in RAM or registers.
Are registers local or global?
The same registers are accessible to all code and a procedure doesn't have access to its own set
of registers.
This might cause overwriting of the resgister your procedure is using.
Need a way to preserve/protect register values!
Second problem:
How to call and then return from a procedure?
Let's say $5 contains important data
Then we call f // procedure f, which might overwrite $5 and then return so we've lost data that
was in $5
Option: divide registers between procedures
- might run out of registers
- won't work for recursion so this is only an option if you're not supporting recursion
Option: make sure each called procedure (callee) guarantees it leaves register values
unchanged
- can overwrite a register, but make sure it backs up the original value and then restore it
where to back up? use RAM!
Where in RAM? Reserve and use a chunk of RAM memory in a systematic way (keep track of
what memory has already been used).
MIPS loader helps set up $30 to just past the last available memory spot (in other words, if you
decrement $30, you'll be pointing to memory that's free).
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The result can be larger than what can be stored in 32 bits so there is no dest register. result is stored in special registers - hi:low (if result within 32 bits, stored in lo, otherwise stored in hi). How to get the result after multiplying? mfhi - move from hi into mflo - move from lo into div , lo stores / (quotient) contains the address of a 32-bit array of integers. contains the number of elements in this array. Note: this is a common start for a problem and then you"ll be asked to do various things. word 4 mult , mflo add , , //

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents