-1

i need to convert a code from java to assembly, but it only prints the first message. but the last two message dosent print neither the message nor the numbers of the result. the two code in java and assembly are that i wrote as follow :

the code in java:

    /**
     * this program count how many 10s can be in a givin number the return the extra or the remain that less than 10
     */
    Scanner input=new Scanner(System.in);
    System.out.println("enter num: ");//print input message
    int num=input.nextInt();// user input 
    int numOf10=0;//counter 
    while(num>9){ //start of while loop
        num-=10;//subtruct 10 from the input number
        numOf10++;//add one to the counter 
       
    }
    System.out.println("number of 10 is: "+numOf10);//print message contain 
    System.out.println("the remain: "+num);//print message contain 

//////////////////////////////////////////////////////////////////////////////////// the code in assembly:

.data
EnterMessage: .asciiz"Enter the number:\n "
ResultMessage: .asciiz"number of 10 is:\n"
remainMessage: .asciiz"the remain:\n "

.text

main:
#ask user to enter input 
li $v0,4
la $a0,EnterMessage
syscall

#read user input
li $v0,5
syscall


#save input
move $t0,$v0

#creat variables
#$t2=9
addi $t2,$zero,9
#counter=$t3=0
addi $t3,$zero,0
#jal loop

#while loop
loop:
ble $t1,$t2,exit

subi $t0,$t0,10
addi $t3,$t3,1
j loop

print:

#print ResultMessage num
li $v0,1
move $a0,$t3
syscall

#print ResultMessage
li $v0,4
la $a0,ResultMessage
syscall


#print remainMessage num
li $v0,1
move $a0,$t0
syscall

#print remainMessage 
li $v0,4
la $a0,remainMessage 
syscall



#close the program
exit:

#end 
li $v0,10
syscall
Michael
  • 57,169
  • 9
  • 80
  • 125
RaOm
  • 1
  • 1

1 Answers1

1

Your problem is here:

ble $t1,$t2,exit

There are a couple of problems here. First, exit leads to:

#close the program
exit:

#end 
li $v0,10
syscall

So you've jumped to the end without doing anything.

Second, you've chosen $t1 as one of your registers for the compare, which you never set up. So its value is currently unknown (most likely the kernel or OS or whatever zeroed all your registers prior to main but it's not a good practice to assume that.)

Let's think about what's being asked:

 while(num>9){ //start of while loop
        num-=10;//subtruct 10 from the input number
        numOf10++;//add one to the counter 
       
    }

When translating a high-level language to assembly we don't have to do everything the same way it was written in the source language. In terms of goto we can think of this as:

loop_begin:
   if (num <= 9) goto loop_exit;
   num -= 10;
   numOf10++;
   goto loop_begin;
loop_exit:

You've got the right idea for the most part. The nice thing about MIPS is that the branching syntax isn't nearly as obtuse as other assembly languages, since you don't have to remember whether carry clear means less than or greater than etc.

loop:
ble $t0,$t2,print  # if $t0 (num) <= 9 goto print
subi $t0,$t0,10    # num = num - 10
addi $t3,$t3,1     # counter++
j loop             # goto loop

Edit: Fixed typo in comments in last section

puppydrum64
  • 1,598
  • 2
  • 15
  • You need to invert/negate the loop exit test condition in the C if-goto-label and assembly version vs. the loop continue condition of the while structured statement form. – Erik Eidt Jan 26 '23 at 14:21
  • Doh! And here I was saying how MIPS had easier branching. I'm a bit rusty it seems. – puppydrum64 Jan 26 '23 at 14:27
  • If you transform the loop to `do{}while()` style, the asm condition matches the C condition. But in this case the loop might need to run zero iterations (unlike with constant trip counts), so you'd also need a ble or blt before the loop to conditionally skip it. [Why are loops always compiled into "do...while" style (tail jump)?](https://stackoverflow.com/q/47783926) – Peter Cordes Jan 26 '23 at 18:28
  • @PeterCordes But I have that in the above example. The first instruction is the `ble` – puppydrum64 Jan 30 '23 at 17:27
  • Your `ble` is *inside* the loop, where it runs every iteration. I'm talking about `if(cond) { do{ ... } while(cond); }` so the actual loop only involves one jump/branch instruction, not two like yours. Read the Q&A I linked for more. – Peter Cordes Jan 30 '23 at 17:29
  • Oh, I see. What you're saying is where the thing we want to do happens once guaranteed, then checks to see if the condition is met before doing it again. – puppydrum64 Jan 30 '23 at 17:31