dimanche 26 septembre 2021

How to include an SLT and jump line in an if-else statement using MIPS 32 assembly?

I'm in an introductory assembly class and I'm trying to figure out how to include an SLT and jump statement in this basic if-else statement in C++, QtSpim gives the right values:

R11 [t3] = 7
R12 [t4] = 1
R13 [t5] = 1

Here's the basic C++ if-else statement the assembly is running:

int i = 0, j = 7, k = 1; //these are arbitrary values for testing

if (i < j) 
  k = k + i; 
else 
  k = k + j; 

Assume: i is in $t0, j is in $t3, and k is in $t4

I'm not sure if this is already correct or if this assembly is good quality, I added a random temp $t5 register for the SLT line and may have added extra jumps. Here's the assembly code I attempted in MIPS 32 using QtSpim:

.text
.globl main

main:
    li $t3, 7          #test val, j = 7
    li $t0, 0          #t0 is counter i = 0
    li $t4, 1         #assign k = 1
    
    slt $t5, $t0, $t3   #set t5 to 1 if i < j
    beq $t5, $0, else   #if t5 == 0 go to else
    add $t4, $t4, $t0   #k = k + i
    j end
    else:
        add $t4, $t4, $t3   #k = k + j
        j end
    
end:
    li $v0, 10   #10 is the code for exit
    syscall

Aucun commentaire:

Enregistrer un commentaire