[英]MIPS32 assembly language basic store and print services
我试图提示用户输入字符串,整数和字符,然后将它们显示回带有标签的屏幕。
我的字符串正常工作,但是字符和整数结果给我带来了垃圾。 这是我的第一个汇编语言程序,不确定为什么将结果保存到$ t寄存器时,为什么要去检索它们却得到了垃圾。
任何帮助,将不胜感激!
.data
stringPrompt: .asciiz "Enter a string up to 25 characters: "
intPrompt: .asciiz "Enter an integer: "
charPrompt: .asciiz "Enter a character: "
theString: .space 26 # max of 25, +1 for '\0'
.align 2
theInt: .space 4
theChar: .space 1
stringMsg: .asciiz "\n\nThe string is: "
intMsg: .asciiz "The integer is: "
charMsg: .asciiz "\nThe character is: "
.text
.globl main
main:
li $v0, 4
la $a0, stringPrompt
syscall
li $v0, 8 # read string service
la $a0, theString # address of buffer for string
li $a1, 26 # read up to 25 & append '\0'
syscall
li $v0, 4
la $a0, intPrompt
syscall
li $v0, 5 # read int service
syscall # $v0 has integer entered after call
move $t0, $v0 # copy (save) integer to $t1
li $v0, 4
la $a0, charPrompt
syscall
li $v0, 12 # read char service
syscall # $v0 has char (gen) entered after call
move $t3, $v0 # copy (save) char to $t3
la $t9, theChar
sb $v0, 0($t5) # save gen to mem (@ gen_charInMem)
###output all data with labels####
li $v0, 4
la $a0, stringMsg
syscall
li $v0, 4
la $a0, theString
syscall
li $v0, 4
la $a0, intMsg
syscall
move $a0, $t1
li $v0, 1
la $a0, theInt
syscall
li $v0, 4
la $a0, charMsg
syscall
move $a0, $t5
li $v0, 11
la $a0, theChar
syscall
li $v0, 10 # graceful exit service
syscall
#################################################
您似乎误解了系统调用1和11的工作方式。 他们希望这些值以$a0
输出-而不是存储值的地址。
所以这:
move $a0, $t1
li $v0, 1
la $a0, theInt
syscall
应该:
move $a0, $t0 # $t0 is where you saved the integer earlier
li $v0, 1
syscall
和这个:
move $a0, $t5
li $v0, 11
la $a0, theChar
syscall
应该:
move $a0, $t3 # $t3 is where you saved the character earlier
li $v0, 11
syscall
这应该一起删除:
la $t9, theChar
sb $v0, 0($t5) # save gen to mem (@ gen_charInMem)
(如果由于某种原因您仍想将字符保存在theChar
,则至少应将第一行更改为la $t5, theChar
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.