bilderimkopf
bilderimkopf

Reputation: 13

MIPS printing string

So here is the practice question:

Given the data segment below, write the code to print the string “Hello”

.data
     .asciiz    “A”
     .asciiz    “Hello”
.globl main
main:

What I wrote under main:

main:
lui $a0, 0x1001
addi $v0, $0, 4
syscall

The output I am receiving is "A", obviously it's because the lui address is wrong. My question is, how do I print "Hello". Do I increment the lui address? And if so, by what?

I have searched around for similar answers, unfortunately people are smart and use pesudo instructions, which I am not able to use.

Thank you in advance for your help.

Upvotes: 1

Views: 29797

Answers (1)

user35443
user35443

Reputation: 6413

It's a good practice to define labels and avoid using unknown constants as adresses in the code. Your code could be rewritten to

.data
     str1: .asciiz    “A”
     str2: .asciiz    “Hello”
.globl main
main:
    lui $a0, $str2
    addi $v0, $0, 4
    syscall

But to answer your question, ASCII A (0x41) takes a single byte, null terminating the first string takes another one, so that Hello should be two bytes above A. The problem here is that your compiler may try to align your data. Better check your compiler options to be completely sure.

Upvotes: 3

Related Questions