首页 > 解决方案 > Mips,打印字符

问题描述

所以基本上,我正在尝试将 C 程序转换为 MIPS,字符数组是字节数组,而不是单词!程序结束时,结果指针必须存储在内存中。而且我的打印有一些问题。所以这是原始的 C 代码。

int main()
{
  char string[256];
  int i=0;
  char *result = NULL;  // NULL pointer is binary zero
  scanf("%255s", string); 
  while(string[i] != '\0') {
    if(string[i] == 'e') {
      result = &string[i]; 
      break; // exit from while loop early
    }
    i++;
  }
  if(result != NULL) {
    printf("First match at address %d\n", result);
    printf("The matching character is %c\n", *result);
  }
  else
    printf("No match found\n");
}

这是我的 Mips 代码。打印字符有问题。而且我不确定代码的逻辑是否正确,请帮我纠正


.globl main 
.text   
main:
    #$s0 : base address string
    #$s1 : i
    #$s2 : *result

    la $s0, string
    addi $s1, $zero, 0 
    addi $s2, $zero, 0 #*result = null  


    #get input from user
    li $v0 ,8 # take input
    la $a0, string # load byte space into address
    li $a1, 256 #allot the byte space for string
    #move $t0, $v0 # # syscall results returned in $t0
    syscall
    add $s0, $a0, $zero # set string = $v0
while:
    add $t9, $s0, $s1 # &string[i]
    lb $t8,0($t9) #t8 = string [i]
    beq $t8 , 0, outsidewhile # if string[i] == '\0'
    beq $t8, 101, body # if string i == 'e'
    add $s1, $s1,1 #i++
    j while
body: 
    add $t2, $t9, $zero #result = &string[i];   
    j outsidewhile # exit from while loop

outsidewhile :
    beq $s2, 0, printaddress # if(result != NULL)
    # printf("No match found\n");
        li $v0,4
        la $a0, msg2        
        li $v0,4
        la $a0, newline 
printaddress:
    #printf("First match at address %d\n", result);

    #display message 
    li $v0,4
    la $a0, msg
    syscall

    li $v0,1
    move $a0, $t2 #print result
    syscall
    #printf("The matching character is %c\n", *result); 


    li $v0,4
    la $a0, msg1
    syscall

    li $v0,11
    move $a0, $s2 # print *result
    syscall

li $v0, 10 
syscall

.data
string: .space 256
msg: .asciiz "\nFirst match at address "
msg1: .asciiz "\nThe matching character is "
msg2: .asciiz "No match found"

标签: cmips

解决方案


推荐阅读