Reputation: 2053
This is my first project sorry for my lack of knowledge, this is the code I'm trying to replicate.
int A[3] = {0, 1, 2};
int x;
for ( int i = 0; i < 3; i++ )
{
if ( A[i] > 1 )
x = 1;
else
{
switch ( A[i] )
{ case 0:
x = 2;
case 1:
x = 3;
}
}
printf(“%d”, x);
}
This is the code that I have in assembly.
main: li $s0, 0
sw $s0, ($s7) #int A[0] = {0};
li $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
li $s0, 2
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 1 #initialize x = 1
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
start: blt $s0, $s2, for
j finish
for: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7
bgt $t0, $s3, exone #a[i] is greater than 1 how to increment the array up?
#switch statement
lw $t0, 0($t1)
jr $t0
j start
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j start
finish: li $v0, 10 # exit system call
syscall
I'm not sure where I'm going wrong it compiles but doesn't give me the output I want or any output for that matter.
Based on some info I have updated my code.
main: add $s0,$zero, $zero
li $s7, 200
sw $s0, 0($s7) #int A[0] = {0};
addi $s0, $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
addi $s0, $s0, 1
sw $s0, 8($s7) #int A[2] = {2};
li $s1, 0 #initialize x = 0
li $s0, 0 #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
#check to enter the for loop
for: blt $s0, $s2, enter
j finish
#enter the for loop
enter: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7 #reg A[i]
lw $t2, 0($t0)
bgt $t2, $s3, exone #a[i] is greater than 1 check
#switch statement
jr $t2
#just in case jump back to for loop
j for
#address for the switch statements
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
#address for the if else statement
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j for
finish: li $v0, 10 # exit system call
syscall
The output should be “231”.
Upvotes: 1
Views: 2935
Reputation: 2053
.data
array: .word 0 : 3
.text
.globl main
main: la $s7, array
add $s0, $zero, $zero
sw $s0, 0($s7) #int A[0] = {0};
addi $s0, $s0, 1
sw $s0, 4($s7) #int A[1] = {1};
addi $s0, $s0, 1
sw $s0, 8($s7) #int A[2] = {2};
add $s1, $zero, $zero #initialize x = 0
add $s0, $zero, $zero #initialize i = 0
li $s2, 3 # constant 3
li $s3, 1 # constant 1
#check to enter the for loop
for: blt $s0, $s2, enter
j finish
#enter the for loop
enter: add $t1,$s0,$s0 #reg $t1 = 2*i
add $t1,$t1,$t1 #reg $t1 = 4*i
add $t0,$t1,$s7 #reg A[i]
lw $t2, 0($t0)
bgt $t2, $s3, exone #a[i] is greater than 1 check
#switch statement I would like to know how to properly do this with a jr
beq $t2, $zero, L0
beq $t2, $s3, L1
#just in case jump back to for loop
j for
#address for the switch statements
L0: add $s0, $s0, 1
li $s1, 2
j print
L1: add $s0, $s0, 1
li $s1, 3
j print
#address for the if else statement
exone: add $s0, $s0, 1
li $s1, 1
j print
print: li $v0, 1 # print integer
move $a0, $s1 # what to print is stored at $s1
syscall
j for
finish: li $v0, 10 # exit system call
syscall
So this works it wasn't exactly how I wanted to do it but it works I wanted to use a jr in the switch statement but I'm not quite there yet.
Upvotes: 0
Reputation: 30480
# [snip]
add $t0,$t1,$s7 #reg A[i]
lw $t2, 0($t0)
bgt $t2, $s3, exone #a[i] is greater than 1 check
#switch statement
jr $t2
This is wrong jr
means Jump Register and it jumps to the address contained in $t2
( which at this point contains a[i]
, which here is 0, 1 or 2 -- not great addresses to jump to).
You could do a lot of things to correct this (I'll assume you meant to have break
statements in your C code), but here's some quick untested code that mimics the switch with a set of if-statements:
li $t3, 0
beq $t2, $t3, L0 # if (a[i] == 0) goto L0
li $t3, 1
beq $t2, $t3, L1 # if (a[i] == 1) goto L1
j print # else fall through
Upvotes: 1
Reputation: 1009
Pumphouse:
First be sure what you want to do,your program does not have any proper aim.
All though you should have to note some points like,
Variables should be declared always on the top of program
not in the loop. variables of same type should be declare together.
Upvotes: 0