Tuesday, March 17, 2020

MIPS read & print string

This is a sample MIPS assembler code to read a string from the user, save it in a buffer and finally print it! The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.

# ---------------------------------------------------------------- #
.data

szStringEntered: .space 20
szEnterString:   .asciiz "Enter string: "
szYouWrote:      .asciiz "You wrote: "

# ---------------------------------------------------------------- #

.text

main:

# ................................................................ #

    la $a0, szEnterString   # load the szEnterString string address
    li $v0, 4
    syscall                 # print it!

# ................................................................ #

    la $a0, szStringEntered # load the szStringEntered string address
    move $t0, $a0           # Keep the address for later use in t0
    li $a1, 20              # max 20 characters
    li $v0, 8               # read string syscall
    syscall                 # Read it!

# ................................................................ #

    la $a0, szYouWrote      # load the szYouWrote string address
    li $v0, 4               # print string syscall
    syscall                 # print it!

# ................................................................ #

    move $a0, $t0           # the szStringEntered address is kept by $t0
    li $v0, 4               # print string syscall
    syscall                 # print it!

# ................................................................ #

    li $v0, 10              # end program syscall
    syscall                 # Do it! Return to OS

# ---------------------------------------------------------------- #


Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment