MIPS32汇编语言基本存储和打印服务

时间:2014-10-15 17:50:34

标签: assembly mips32

我试图提示用户输入字符串,整数和字符,然后用标签将它们显示回屏幕。

我的字符串工作正常,但字符和整数结果给了我垃圾。这是我的第一个汇编语言程序,我不确定为什么当我将结果保存到$ 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 个答案:

答案 0 :(得分:0)

您似乎误解了syscalls 1 and 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

这应该被删除altoghether:

    la $t9, theChar
    sb $v0, 0($t5)          # save gen to mem (@ gen_charInMem)

(如果您仍想出于某种原因将字符保存在theChar,则至少应将第一行更改为la $t5, theChar。)