Access last two digits of hex number in MIPS

0

I want to print out the last two digits of a hexadecimal number in MIPS. I have this code but the output is 0x00000000 when I expected the output to be 0x00000021. Where am I going wrong?

.data
num:  .word 0x00654321
.text
la $s0, num # make s0 equal to num
andi $a0, $s0, 0x00000011 # do bitwise AND
li $v0 34 # print result in hex
syscall
assembly
hex
mips
bitwise-operators
asked on Stack Overflow Mar 10, 2021 by tdayron • edited Mar 10, 2021 by Michael

1 Answer

0

la $s0, num gives you the address of num, not the value stored there. The instruction you want is lw.

And when you mask you should use 0xFF to get the two least significant hex digits, not 0x11. Using 0x11 would only give you the least significant bit of those digits, e.g. 0x21 & 0x11 = 0x01.

answered on Stack Overflow Mar 10, 2021 by Michael

User contributions licensed under CC BY-SA 3.0