0

How would I properly iterate through the characters of a defined string in Assembly? I have tried:

lb $t2, $t1($t0)
#$t1 is a number and $t0 is the string

However this does not work because $t1($t0) is illegal syntax.

  • You need to add them together first. If you don't need to index just iterate, you can of course use `($t0)` and increment it in a loop. – Jester Feb 14 '22 at 20:48
  • How would I go about doing that? Whenever I try to do lb $t1, ($t0) $t1 is set to 0. – Bradley Lund Feb 14 '22 at 21:44
  • `addu $t2, $t0, $t1; lb $t2, ($t2)` for example. – Jester Feb 14 '22 at 21:52
  • Sorry, I'm rather new to Assembly. Could you please elaborate on what this does exactly? I'm familiar with add unsigned and load byte, but not in the way it's used in this context. – Bradley Lund Feb 14 '22 at 21:59
  • It simply adds `$t0` and `$t1` then loads the byte from that location. Note these are 2 separate instructions. – Jester Feb 14 '22 at 22:10
  • If you want to do something but the instruction set will not do it in one instruction, then use a sequence of instructions instead. – Erik Eidt Feb 14 '22 at 22:13
  • $t0 is declared by using la $t0, input_str, where input_str is a string obtained from syscall. Would this code still work correctly? – Bradley Lund Feb 14 '22 at 22:17
  • Read the comments in this thread: https://stackoverflow.com/q/70815971/471129 – Erik Eidt Feb 14 '22 at 22:20
  • Yes, it doesn't care how you got your string. – Jester Feb 14 '22 at 22:38
  • Technically, that's not a declaration, but rather an initialization (and also a repurposing of the register for a new usage). – Erik Eidt Feb 14 '22 at 23:42

0 Answers0