c - 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"
解决方案
推荐阅读
- html - 为什么 `position:relative` 在 safari 和 Opera 的 devtools 中计算为 `position: static`?
- python - JSON,Unicode 或二进制响应
- r - 使用 R 对同一列进行交叉制表的最佳方法
- node.js - 如何修复本地隧道的“隧道服务器脱机:请求失败,状态码为 502”错误
- vba - Windows().Activate 似乎不起作用。如何解决“请求的成员不存在”?
- unity3d - 如何在构建管道期间删除场景中的某些对象?
- iframe - 允许在网站中嵌入视频,同时保持 youtube 在网络中被阻止
- php - 如何将自定义用户搜索请求解析为全文搜索?
- cmd - 使用 'SET' 获取表达式的结果
- scala - 泛型类型的 Scala 组合