简体   繁体   中英

Assembly MIPS: Reverse printing two strings

Lets say that you type a name first and a surname last and you want the program to print the surname first and then the name.

fe

Gabe

Newell

Newell

Gabe

Ι tried to make just that:

.data  
first: .word
second: .word

.text
.globl main 
main: 

li       $v0, 8 
la       $a0, first
la   $a1, 20 
syscall

move     $s1, $a0

la       $a0, second
la   $a1, 20 
syscall

li   $v0, 4
la       $a0, second
syscall
     
move     $a0, $s1

la       $a0, 0($s1)
syscall

li $v0, 10
syscall

but at the output it gives me: newell newell

So, what it the problem here?

You didn't set aside enough space for the names; .word only sets aside enough for an integer (2 or 4 bytes, probably the latter).

Use .space instead.

Then, to print the first name, you need to load the address of first into $a0 before making the syscall ; $s1 does not have the value you think it does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM