我想修理我的mips装配

时间:2014-10-16 09:36:00

标签: assembly mips

我想修理我的mips装配。

这是我的代码。

#include <stdio.h>

 void sort (int v[], int n);

 int main(void)
{
  int arr[5], i, n=5;
  printf("input 5 numbers:"); 
  for( i = 0; i < n; i++)
 {
scanf("%d", &arr[i]);
 } 


  printf("result:"); 
  for( i = 0; i < 5; i++)
  {
printf("  %d", arr[i]);
   } 

    printf("\n"); 
    return 0;
  }

这是我的mips程序集

.data

msg1 : .asciiz "input 5 numbers" 
msg2 : .asciiz "result"

.text
.globl main

main: addi $s0, $zero, arr
      addi $s1, $zero, 5
      add  $a0, $zero, $s0

      li $v0, 4 
      la $a1, msg1
      syscall

      move $t0,$zero 

loop1 : sll $t1,$t0,2 
        addi $t0,$t0,1
        slt  $t2,$t0,5
        li $v0,1
        syscall
        bne $t2, $zero, loop1

       li $v0, 4
       la $a2, msg2 
       syscall

loop2 : sll $t3, $t0, 2
        addi $t0, $t0, 1
        slt $t4,$t0,5
        li $v0, 1
        syscall
        jal  L2

        li $v0, 4

我试图将c代码转换为mips程序集,但是当我使用qtspim时它有很多错误 C代码没问题,但要转换成mips汇编,我不知道哪里出错。 我不擅长mips装配...... 你能修复我的mips汇编代码吗?

1 个答案:

答案 0 :(得分:0)

如果我使用我的工具链转换它,那么结果如下。

    .file   1 "mipc-C.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:
    .ascii  "input 5 numbers:\000"
    .sdata
    .align  2
$LC1:
    .ascii  "%d\000"
    .align  2
$LC2:
    .ascii  "result:\000"
    .align  2
$LC3:
    .ascii  "  %d\000"
    .align  2
$LC4:
    .ascii  "\n\000"
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,56,$31      # vars= 32, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,56
    sw  $31,52($sp)
    sw  $fp,48($sp)
    move    $fp,$sp
    jal __main
    li  $2,5            # 0x00000005
    sw  $2,44($fp)
    la  $4,$LC0
    jal printf
    sw  $0,40($fp)
$L2:
    lw  $2,40($fp)
    lw  $3,44($fp)
    slt $2,$2,$3
    bne $2,$0,$L5
    j   $L3
$L5:
    lw  $2,40($fp)
    move    $3,$2
    sll $2,$3,2
    addu    $3,$fp,16
    addu    $2,$3,$2
    la  $4,$LC1
    move    $5,$2
    jal scanf
$L4:
    lw  $2,40($fp)
    addu    $3,$2,1
    sw  $3,40($fp)
    j   $L2
$L3:
    la  $4,$LC2
    jal printf
    sw  $0,40($fp)
$L6:
    lw  $2,40($fp)
    slt $3,$2,5
    bne $3,$0,$L9
    j   $L7
$L9:
    lw  $2,40($fp)
    move    $3,$2
    sll $2,$3,2
    addu    $3,$fp,16
    addu    $2,$2,$3
    la  $4,$LC3
    lw  $5,0($2)
    jal printf
$L8:
    lw  $2,40($fp)
    addu    $3,$2,1
    sw  $3,40($fp)
    j   $L6
$L7:
    la  $4,$LC4
    jal printf
    move    $2,$0
    j   $L1
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,52($sp)
    lw  $fp,48($sp)
    addu    $sp,$sp,56
    j   $31
    .end    main