I'm trying to code the MIPS code equivalent of this high level language code:
i = 0;
n = str2; //supplied by user from console
while(i < n) {
System.out.println(str1); //str1 is supplied by user from console
i++;
}
System.exit(0);
This is my MIPS code:
.data
str1: .asciiz "Enter the first integer: "
str2: .asciiz "Enter the second integer: "
newline: .asciiz "\n"
.text # instructions follow this line
main: # indicates start of code (first instruction to execute)
add $s0,$zero, $zero # $s0 = 0
add $s1, $zero, str2 # $s1 = 0
slt $t0, $s0, $s1
beq $t0, $zero, Exit
li $v0, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
move $a0, str1 # move integer to be printed into $a0: $a0 = str1
syscall
addi $s0, $s0, 1 # $s0++
j loop #jump back to loop
Exit: nop
I'm trying to print the 1st number the amount of times the value of the 2nd number is. Example: 1st number: 2, 2nd number: 4, so print 2 four times
Your code is missing a "loop" label. I'm going to guess that it should go above the first "add".
You should be using the "u" form of addition and subtraction instructions. That is, "addu" instead of "add", and "addiu" instead of "addi". This is because "add" and "addi" will crash on overflow instead of wrap. Java does not crash on integer overflow, and you wanted the MIPS equivalent of the Java code.
Instead of literally "str1" and "str2", the MIPS code should have the names of the registers into which the user-specified numbers were loaded.
"move $reg1, $reg2" is equivalent to "addu $reg1, $zero, $reg2". In fact, the "move" instruction does not actually exist, and is actually implemented as macro for "addu" with register $zero by the assembler.
Also, are you using an assembler that automatically handles reordering? I'm curious whether your assembler handles the MIPS delay slot for you, or whether you need to put stuff in the delay slot of branches yourself. This can change how you need to code your loop.