0

I would like to print out "HEL" from the string "HELLO!". I am aware that I can load individual byte using lb in MIPS, however, how do I load the first 3 characters?

.data
    string: .asciiz "HELLO!"

.text
    lw $t6, string
    lb $a0, 0-2($t6)  # pseudo-code, returns "HEL"
    li $v0, 4 # as opposed to 11, printing character
    syscall
1
  • There's no system call that prints a string where the actual string contents are stored in a register. You could temporarily modify the string in the data section by storing 0 at the location of the second 'L' and then using system call 4. Or just use three lb instructions and system call 11s. Commented Nov 21, 2021 at 19:24

1 Answer 1

2

This has little to do with MIPS, and everything to do with C style nul-terminated strings, if you consider printing a character and whole string are your only options.

So, you can print the first 3 characters one a a time using syscall #11, or, copy them to another string and print it using syscall #4.

Otherwise, you can modify the string in place by smashing the 2nd L with a nul character, and print it.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I modify the string? I'm reading something like this: stackoverflow.com/questions/56584949/…
Ok, I figured it out using sb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.