簡體   English   中英

MIPS32匯編語言基本存儲和打印服務

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM