-1

I am attempting to convert the number 0x20014924 to a binary number. I am getting an unexpected binary readout:

0000 0001 0011 0001 0110 0111 0100 1100 actual output
0010 0000 0000 0001 0100 1001 0010 0100 expected output.

Any suggestions on debugging or where I am going wrong would be greatly appreciated!

.data #start to define user data

myStr: .asciiz "the answer = " # define a user string

myNum: .word 20014924   # define a user data word
.text                   # indicate that user program starts

.globl main
main:           # the main routine
li $v0, 4       # system call number 4 is for printing strings
la $a0, myStr   # load the starting address of myStr to $a0
syscall         # now print the string

lw $t1, myNum
addiu $t0, $zero, 31    # initialized counter
addi $t2, $zero, 1      # adds 1 to the LSB
sll $t2, $t2, 31        # moves 1 to the MSB
li $v0, 1               # system call number 1 is for printing integers

loop:
beq $t0, -1, exit   # checks the counter
and $t3, $t2, $t1   # $t2 AND $t1 saves to $t3

beq $t3, $0, post_shift     # if the AND results in 0 skip to post shift
srl $t3, $t3, $t0           # if the AND results in 1 srl by counter

post_shift:
move $a0, $t3   # moves value of AND to arg resgister
syscall         # now print the integer

sub $t0, $t0, 1 # decrements counter
srl $t2, $t2, 1 # shifts comparer 1 to the right

j loop
exit:
jr $ra          # main routine returns through jump register
  • 1
    `20014924` is a decimal constant. Try `200F4924` and see the assembler choke on it; you want `0x20014924`. The assembler converts it to binary at assemble time; that's why your code is actually just outputting the binary digits of a binary integer, not converting a string of ASCII hex digits to a (binary) integer. See also [MIPS Assembly converting integer to binary and reading the number of 1's?](https://stackoverflow.com/a/71408954) re: understanding that registers are already binary. – Peter Cordes Mar 09 '22 at 20:58
  • Not quite a duplicate of [Using binary numeric constants in MIPS asm source code?](https://stackoverflow.com/q/9828251) - the *question* there contains the answer to this. Almost a duplicate of [Why are hexadecimal numbers prefixed with 0x?](https://stackoverflow.com/q/2670639) or [How to represent hex value such as FFFFFFBB in x86 assembly programming?](https://stackoverflow.com/q/11733731) – Peter Cordes Mar 09 '22 at 21:09

1 Answers1

0

Figured out I needed to represent the word in hexadecimal

myNum: .word 0x20014924   # define a user data word