MIPS字符数组从c ++中反转翻译

时间:2018-03-20 20:57:21

标签: assembly mips mips32

我的项目是转换下面的代码,它反转字符串的顺序,即(字符串 - > gnirts)。我不知道如何开始转换为MIPS汇编。任何帮助或澄清如何做这个项目将不胜感激。即使只是一个类似的例子将是很棒的。

#include <iostream>
using namespace std;

void reverse(char [], int size);
void swap(char *x, char *y);

int main() 
{

char array[] = "0123456789";
cout << array << endl;
reverse(array, 10);
cout << "Reverse:" << endl;
cout << array << endl;
return 0;

}


//Takes in a character array and the size of set array and loops through 
until 
you reach a middle point
void reverse(char array[], int size) 
{
int j=size-1;
for(int i=0; i<j; i++) 
{
    swap(&array[i], &array[j]); //swaps the first and last element by 
passing the memory locations
    j--;
}
}

//Takes pointers to the closer element and swaps it into the farther element
void swap(char *x, char *y) 
{
char temp = *x; //first element stored in temp
*x = *y; //last element sored in first
*y = temp; // temp stored in last
 }

1 个答案:

答案 0 :(得分:1)

以下是reverseswap函数。您需要添加设置和打印的main

# reverse -- reverse elements of a byte array
#
# arguments:
#   a0 -- pointer to array
#   a1 -- length of array (bytes)
#
# temporary:
#   a1 -- pointer to last char
#   t0 -- return address
reverse:
    move    $t0,$ra                 # save return address

    addu    $a1,$a0,$a1             # point past the array (&array[SIZE])
    addiu   $a1,$a1,-1              # point to last element of array
    j       reverse_next            # start loop

reverse_loop:
    jal     swap
    addiu   $a0,$a0,1               # move LHS pointer higher
    addiu   $a1,$a1,-1              # move RHS pointer lower

reverse_next:
    blt     $a0,$a1,reverse_loop    # more to do? loop if yes
    jr      $t0                     # return

# swap -- swap elements of a byte array
#
# arguments:
#   a0 -- pointer to LHS of array
#   a1 -- pointer to RHS of array
#
# temporary:
#   v0 -- LHS char
#   v1 -- RHS char
swap:
    lb      $v0,0($a0)              # get LHS byte
    lb      $v1,0($a1)              # get RHS byte

    sb      $v1,0($a0)              # store LHS byte
    sb      $v0,0($a1)              # store RHS byte

    jr      $ra                     # return

<强>更新

请参阅我在其他页面上关于运行时异常的评论(例如,您将索引传递给swap而不是指针)。

在这里,我重新编码了reverse函数以使用指针。这是C等价物:

void
reverse(char *array, int size)
{
    char *endp;

    // this could be "endp = &array[size - 1]" but this is closer to the
    // assembly
    endp = &array[size];
    endp -= 1;

    for (;  array < endp;  ++array, --endp)
        swap(array,endp);
}

更新#2:

剧透警报:这是完整的程序......

    .data
prompt:     .asciiz     "Enter string to reverse: "
string:     .space      1000

    .text
    .globl  main
main:
    li      $v0,4                   # print syscall
    la      $a0,prompt
    syscall

    li      $v0,8                   # read string
    la      $a0,string
    li      $a1,1000
    syscall

    la      $a0,string
    jal     strlen                  # get string length

    move    $a1,$v0                 # save string length
    la      $a0,string
    jal     reverse

    li      $v0,4                   # print string
    la      $a0,string
    syscall

    li      $v0,10                  # exit
    syscall

# reverse -- reverse elements of a byte array
#
# arguments:
#   a0 -- pointer to array
#   a1 -- length of array (bytes)
#
# temporary:
#   a1 -- pointer to last char
#   t0 -- return address
reverse:
    move    $t0,$ra                 # save return address

    addu    $a1,$a0,$a1             # point past the array (&array[SIZE])
    addiu   $a1,$a1,-1              # point to last element of array
    j       reverse_next            # start loop

reverse_loop:
    jal     swap
    addiu   $a0,$a0,1               # move LHS pointer higher
    addiu   $a1,$a1,-1              # move RHS pointer lower

reverse_next:
    ble     $a0,$a1,reverse_loop    # more to do? loop if yes
    jr      $t0                     # return

# swap -- swap elements of a byte array
#
# arguments:
#   a0 -- pointer to LHS of array
#   a1 -- pointer to RHS of array
#
# temporary:
#   v0 -- LHS char
#   v1 -- RHS char
swap:
    lb      $v0,0($a0)              # get LHS byte
    lb      $v1,0($a1)              # get RHS byte

    sb      $v1,0($a0)              # store LHS byte
    sb      $v0,0($a1)              # store RHS byte

    jr      $ra                     # return

# strlen -- get string length
# RETURNS:
#   v0 -- string length
#
# arguments:
#   a0 -- pointer to string
#
# clobbers:
#   t1 -- current char
strlen:
    move    $v0,$a0                 # remember base address

strlen_loop:
    lb      $t1,0($a0)              # get the current char
    addi    $a0,$a0,1               # pre-increment to next byte of string
    bnez    $t1,strlen_loop         # is char 0? if no, loop

    subu    $v0,$a0,$v0             # get length + 1
    subi    $v0,$v0,1               # get length (compensate for pre-increment)
    jr      $ra                     # return