1

hey friends im new to mips and im trying to convert numbers from a linked list into base 4 and print them out but i dont really know how to do that i have heared about a way with dividing the numbers by 4 and pushing the remainder of each number to a somekind of stack but im unsure how to do that or which stack i should use

i want to print out each number in the list as base 4 number

i would love to get some help or guidance how to do it

here is my code so far (im printing the sum of the numbers too and the sum of the postive numbers whos divided by 4)

   .data
num1: .word -1 , num3
num2: .word 17 , 0
num3: .word 32 , num5
num4: .word -6 , num2
num5: .word 1972 , num4 

.globl main 
.text
main:  
la   $t1,num1
li   $s0,0      # sum of the linked list
li   $s1,0      # sum of divided by 4 and postivie in the list
li   $t3,0      # temp remainder for the div by 4
li   $t4,4
li   $t5,0      # counter how many words we enterted the stack

sumloop: 
beqz     $t1,exit
lw   $t0,0($t1) 
add  $s0,$s0,$t0    # adding to the sum the value in the current node
lw   $t1,4($t1)

### need your help here :)

blez     $t0,sumloop    # if negative or zero dont add to s1
div      $t0,$t4
mfhi     $t3
bnez     $t3,sumloop    # if the value isnt divided by 4 without remainder jump to sumloop
add  $s1,$s1,$t0
j    sumloop

exit:
move     $a0,$s0
li   $v0,1
syscall 

li   $a0,'\n'
li   $v0,11
syscall 

move     $a0,$s1
li   $v0,1
syscall 
dor
  • 25
  • 1
  • 7
  • 2
    The numbers are already encoded in base 2, so you don't need to literally divide it, every two bits form a base 4 digit. You can use shift + mask (by `and`) to extract two bits every time, and convert them into digit. Last digit is also remainder of "divide by 4", so you can test if last digit is 0 and add to `s1` only then. The negative values make these assumptions a bit more tricky (for example -4 is 11...1100). – Ped7g Aug 02 '18 at 20:11
  • And because the values are already basically ready to be printed out in base 4 without calculation, you can simply print form left to right without stack, just skip the leading zeroes (and make sure you output last zero if whole value is zero). Or if you can print fixed length with leading zeroes, then there's no need to skip them. You should try to code this one first, as it's simpler, then add the leading-zeroes skip. – Ped7g Aug 02 '18 at 20:13

0 Answers0