0

So I can manage to get the user inputted integer into $v0, then I use

la $t0 ($v0)

and I store the integer entered into $t0. How would I try to get the first byte of the interger from $t0. Every time I try to use

lb $t1 0($t0)

I get an error: Exception 7 [Bad data address] occurred and ignored.

Michael
  • 57,169
  • 9
  • 80
  • 125
Torched90
  • 305
  • 1
  • 3
  • 18

1 Answers1

0

The lb instruction is used to read a byte from memory. That is, lb $t1, 0($t0) would attempt to read a byte from memory at the address stored in $t0 (it would also sign-extend that byte, which may not be what you want).

What you want to do is not to read from memory, but isolate the least significant byte of a value already stored in a register. You can do that with the andi instruction:

andi $t1, $t0, 0xFF  # $t1 = $t0 AND 0xFF

You should read up on bitwise logic, since such operations are quite common in assembly language code.

Michael
  • 57,169
  • 9
  • 80
  • 125