1

I'm trying to learn some assembly, and following this guide.

I'm at exercise 3, which sounds like this:

Given n (stored in rdi) values stored in IN:

1. For each of BYTE value x taken from IN:
i. If x > 0x50, write x - 0x37 to OUT.
ii. Else, write x + 0x13 to OUT.
Hint: Note that values from 0x80 to 0xff are considered as negative values when they are a BYTE (because MSB is set). Make sure you use the unsigned versions of the jcc instructions.
To simplify your experience, r8 is initialised to 0x1000(IN), and r9 to 0x2000(OUT).

So my thought was that I should use the loop operator, and that I should use jg to test for a greater than comparison:

loop:
mov r10, r8
cmp r8, 0x50
jg great
not_great:
 sub r10, 0x37 
 mov r9, r10
great:
 add r10, 0x13
 mov r9, r10
end:
 hlt

jmp loop

There's probably a few issues here. But the first one is that I think I am just reading the same value from r8 again and agai, not iterating over them. How do I actually iterate over them? so I don't hit the same value again and again?

Grazosi
  • 603
  • 1
  • 10
  • To iterate you need to 1) increment `r8` in the loop and 2) dereference it, e.g. `cmp byte [r8], 0x50`. Also obviously having a `hlt` there will prohibit any iteration and the `jmp` should instead be a conditional that check whether you processed `n` items or not. – Jester Feb 24 '22 at 23:57
  • Hmm, yeah, but that's what I really can't figure out how to do. The dereferencing and so on – Grazosi Feb 25 '22 at 00:06
  • 1
    To iterate over the 8 bytes in a single qword register, you can `shr r10, 8` (or `ror r10, 8`) in a loop, and `cmp r10b, 0x50` or whatever else with the low byte of the register. Or even extract it with `movzx eax, r10b`. Like in this int->hex-string loop, where we want to loop over nibbles: [How to convert a binary integer number to a hex string?](https://stackoverflow.com/q/53823756) although that just copies the register and uses `and` to keep only the low 4 bits. – Peter Cordes Feb 25 '22 at 01:18
  • 1
    But I don't understand what it means to "write `x - 0x37` to `OUT`. That sounds more like dealing with an array, unless they really mean just doing a `mov` store of different bytes to a specific memory address, like it was a device MMIO register so each write has some visible side-effect, not just the final value. – Peter Cordes Feb 25 '22 at 01:20
  • 1
    Probably they are talking about looping over arrays pointed-to by a register (like Jester said), not looping over the bytes literally *in* a single register like your question title said. – Peter Cordes Feb 25 '22 at 01:21

0 Answers0