Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS to C Translation

I need to translate some MIPS assembly instructions to C code. I think I got it, but it seems counter-intuitive. Any help? We have variables f, g, h, i, j stored in registers $s0, $s1, $s2, $s3 and $s4 respectively. The base of arrays A and B are stored in $s6 and $s7 respectively. 4 byte words. Comments in the code are my own.

addi $t0, $s6, 4   # $t0 = A[1]
add $t1, $s6, $0   # $t1 = A[0]
sw $t1, 0($t0)     # $t0 = A[0]
lw $t0, 0($t0)     # $t0 = A[0]
add $s0, $t1, $t0  # f = A[0] + A[0]

I just feel like I'm wrong. Why make $t0 A[1] first if we never use it?

like image 964
user2789564 Avatar asked Sep 19 '13 02:09

user2789564


2 Answers

I think you have got in completely wrong.

addi $t0, $s6, 4 # $t0 = A[1]

After the addi, register $t0 became the memory address of A[1], which would be &A[1], not A[1]. To get value of A[1], you need to use lw after you done the addi

lw $t0, 0($t0) # $t0 =A[1]

like image 105
TheCyberliem Avatar answered Oct 02 '22 06:10

TheCyberliem


sw $t1, 0($t0)     # $t0 = A[0]

You've got this back-to-front. It is a store, so it is used:

sw $t1, 0($t0)     # A[1] = $t1
like image 41
Jens Björnhager Avatar answered Oct 02 '22 07:10

Jens Björnhager