简体   繁体   中英

Translating a C statement into MIPS assembly instructions?

I have another homework question that, like the last question I asked on this site, wasn't explained well by the teacher nor the textbook. Here's the question:

Translate this C statement into MIPS assembly instructions:

   B[8] = A[i-j];

Assume variables f, g, h, i and j and are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume the base addresses of the arrays A and B are in registers $s6 and $s7, respectively.

Now, where I'm stuck is adding the two variables and using the result as an offset. So far, I have the following:

sub $t0, $s3, $s4 # add values to get offset amount, store in $t0
sll $t1, $t0,2    # multiply the offset by 4, store in $t1

Now, I don't know if I can use $t1 as an offset to access that array element. It looks like the textbook only uses numbers (eg 4($s7) ) instead of registers (eg $t1($s7) ) What do I do next?

A compiler can only translate complete program, so here's a complete program that includes your instruction, with its translating into MIPS asembly instrution. I hope you can study what's being done here and draw some conclusions.

int main() {

int i = 3;
int j = 2;
int B[3] = {10, 20, 30};
int A[3] = {100, 200, 300};

B[8] = A[i-j];
}

MIPS for the above:

    .file   1 "Cprogram.c"

 # -G value = 8, Cpu = 3000, ISA = 1
 # GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
 # options passed:  -msoft-float
 # options enabled:  -fpeephole -ffunction-cse -fkeep-static-consts
 # -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
 # -meb -mcpu=3000

gcc2_compiled.:
__gnu_compiled_c:
    .rdata
    .align  2
$LC0:
    .word   10
    .word   20
    .word   30
    .align  2
$LC1:
    .word   100
    .word   200
    .word   300
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,64,$31      # vars= 40, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,64
    sw  $31,60($sp)
    sw  $fp,56($sp)
    move    $fp,$sp
    jal __main
    li  $2,3            # 0x00000003
    sw  $2,16($fp)
    li  $2,2            # 0x00000002
    sw  $2,20($fp)
    addu    $2,$fp,24
    la  $3,$LC0
    lw  $4,0($3)
    lw  $5,4($3)
    lw  $6,8($3)
    sw  $4,0($2)
    sw  $5,4($2)
    sw  $6,8($2)
    addu    $2,$fp,40
    la  $3,$LC1
    lw  $4,0($3)
    lw  $5,4($3)
    lw  $6,8($3)
    sw  $4,0($2)
    sw  $5,4($2)
    sw  $6,8($2)
    lw  $2,16($fp)
    lw  $3,20($fp)
    subu    $2,$2,$3
    move    $3,$2
    sll $2,$3,2
    addu    $3,$fp,16
    addu    $2,$2,$3
    addu    $3,$2,24
    lw  $2,0($3)
    sw  $2,56($fp)
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,60($sp)
    lw  $fp,56($sp)
    addu    $sp,$sp,64
    j   $31
    .end    main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM