Search code examples
assemblymips32

MIPS32 assembly language basic store and print services


I am trying to prompt the user for a string, integer, and character, and then display them back to the screen with labels.

I have the string working properly, but the character and integer result gives me garbage. It is my first assembly language program and am not sure why when i save my results to the $t registers, that when i go to retrieve them i get garbage.

Any help would be appreciated!

        .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
#################################################

Solution

  • You seem to have misunderstood how syscalls 1 and 11 work. They expect the values to output in $a0 - not the addresses where the values are stored.

    So this:

        move $a0, $t1
        li $v0, 1
        la $a0, theInt
        syscall
    

    should be:

        move $a0, $t0   # $t0 is where you saved the integer earlier
        li $v0, 1
        syscall
    

    And this:

        move $a0, $t5
        li $v0, 11
        la $a0, theChar
        syscall
    

    should be:

        move $a0, $t3   # $t3 is where you saved the character earlier
        li $v0, 11
        syscall
    

    This should be removed altoghether:

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

    (If you still want to save the character at theChar for some reason, you should at least change the first line to la $t5, theChar).