1

I have some code that needs to be translated into Assembly. I am new to Assembly and I get confused and I think its pretty hard to write. I've gone through some tutorials but I am still getting problems when I compile the code. I need some help - Here is the original code and my code in Assembly. I am sure how to fix the issues: I gave some explanation of what I am trying to do in the Assembly code - I need to know if I am way off

Original Code:

int A[4] = {0, 1, 2, 3}; 
int x;
for ( int i = 0; i < 4; i++ ) 
{ 
   if ( A[i] < 1 ) 
         x = 1; 
   else 
   { 
      switch ( A[i] ) 
      { 
         case 0: 
          x = 2; 
         case 1: 
          x = 3; 
         case 2:
          x = 5; 
         case 3: 
          x = 7;
      } 
   } 
   printf(“%d ”, x);
}

My Assembly Code

.data 
jumpTab: .word lab0, lab1, lab2, lab3 #Jumptable
nums: .word 0,1,2,3 #array declaration of integers
numberx: .ascii "X equals: "

.text
 main:

 addi $t0, $0, 0 #Declare I
 addi $t1, $0, 0 #Declare X
 ori  $t2, $t0, 4 # Constant Value 4
 ori  $t4, $t0, 1   # Constant Value 1
la $t3, nums    #initialize Array Nums


forloop: bgt $t0, $t2 end  #branch if i > four to end:
 add $t0, $t0, $t0 Loop:  #add i++ and go to loop:

loop: bgt  $t3, $t4 Else  #if Array Index (i) is more than 1 constant 1 branch 
   add $t1, $t1, 1

Else: 
la $t5, JumpTable
sll $t5, $t6, 2 #offset
jr $t7

lab0:
   la $t1, 2   #load address set x=2
   j Print
lab1:
   la $t1, 3     #load address set x=3
   j Print
lab2:
    la $t1, 5   #load address set x=5
    j Print
lab3:
    la $t1, 7   #load address set x=7
    j Print

 print: #print out X = 

 li $v0, 4
   syscall


    end:   
timrau
  • 22,578
  • 4
  • 51
  • 64

0 Answers0