Why load address works different on words and labels with strings?

158 views Asked by At

​Hi,

I just had a quick question. Let's say I have 2 labels string and word with string being a label associated with a string and word being an actual word with a 32 bit integer stored in it. Now let's say I use a syscall to print string using the label string and I use load address with register a0, it will print the string. However why doesn't it print the address, since this is load address. On the other hand if I do the exact same thing but instead of using the string label I use the word label I am now instead going to print the actual address. I was wondering why it works this way.

Thanks for your time

Varun G.

1

There are 1 answers

1
gusbro On BEST ANSWER

I think you are misinterpreting syscalls. You issue a certain syscall to do different things.

To print a string you issue a syscall 4 ($v0 = 4). This service prints the string pointed by the address given in $a0 which you should previously load.

To print an integer you issue a syscall 1 ($v0 = 1). This service prints the integer stored at $a0. This integer may be seen as an address if you have loaded it with la pseudoinstruction.

So if you have the following snippet:

.data
str: .asciiz "hello!"

.text
  la $a0, str
  li $v0, 1
  syscall  # This will print the starting address of the string
  li $v0, 4
  syscall  # This will print the contents of the string pointed by label str

it will print the address of the beginning of the string and also its contents.