user3228356
user3228356

Reputation: 1

Can't get the output of following MIPS assembly code

I'm trying to print characters one by one in a string array using MIPS. but it only prints syscall. please help

.text
main:
    la $a0, myarray
    li $v0, 4
    syscall

    lbu $t0,0($a0)
    lbu $t1,0($a0)
    lbu $t2,0($a0)
    lbu $t3,0($a0)

    jr $ra
    .data
    myarray:.asciiz "Hello\n"

Upvotes: 0

Views: 290

Answers (1)

Omar Darwish
Omar Darwish

Reputation: 1655

This definitely could have been done in a more succinct way, but I hope you get the idea!

.data
     myarray: .asciiz    "Hello\n"
     newline: .asciiz     "\n"
.text 
main:
     la $a0, myarray        #load address of original string
     li $v0, 4                #syscall for print string
     syscall

     la $s1, myarray        #save base address of string
     lb $a0, ($s1)         #load 1st char byte as arg
     jal printchar           #print char and return

     addi $s1, $s1, 1         #increment address to next char byte
     lb $a0, ($s1)         #load 2nd char byte as arg
     jal printchar           #print char and return

     addi $s1, $s1, 1     #increment address to next char byte
     lb $a0, ($s1)         #load 3rd char byte as arg
     jal printchar           #print char and return

     addi $s1, $s1, 1         #increment address to next char byte
     lb $a0, ($s1)         #load 4th char byte as arg
     jal printchar           #print char and return

     addi $s1, $s1, 1         #increment address to next char byte
     lb $a0, ($s1)         #load 5th char byte as arg
     jal printchar           #print char and return

     li $v0, 10            #syscall for exit
     syscall

printchar:                #expects that charater byte is loaded into $a0
     li $v0, 11            #syscall for printchar
     syscall

     la $a0, newline        #load address of new line string
     li $v0, 4             #syscall for print string
     syscall

     jr $ra                #return to main

Upvotes: 2

Related Questions