0

Hope this is not a question too simple/confus but I could never find out how I know what addresses to pick. I know there are a lot but how do I know an address that guaranteed isn't used by any other program? Let's say i am writing a simple NASM program and need to save 32 individual bytes in storage, how do I know where to save them? I need them to be random access, so the 15th one would be address 0 + 15

Or is this a horrible way to store data in memory? Trying to use as few external libraries as possible - how would I go about storing such data?

Maritn Ge
  • 997
  • 8
  • 35
  • For global variables you can put something like `label: times 32 db 0` in a data section (writable and not in the path of execution). – ecm May 04 '22 at 20:10
  • 3
    You can create temporary storage on the stack by simply subtracting 32 from the current stack-pointer address to create 32-bytes of storage on the stack. You can use random access to any of the storage bytes from the current value in the stack pointer plus an offset. When done using the memory, just add 32 back to the stack pointer value. – David C. Rankin May 04 '22 at 22:50
  • 3
    Note, the 15th one would be `addr + 14` (the 1st one is at `0` offset) – David C. Rankin May 04 '22 at 23:55
  • @DavidC.Rankin can i use the stack in _64 nasm? i thought push does not work there or something – Maritn Ge May 05 '22 at 12:51
  • 1
    It works the same no matter whether it's x86 or x86_64. Here are two short examples [Nasm x86_64 Use Stack For Temp Storage](https://pastebin.com/e3nUyLWk) and [Same with x86](https://pastebin.com/4wGYHYXN). They show both the use of a global string containing the alphabet, creating 32-bytes of temp storage on the stack. copying the global to the temp stack space, accessing the 15th char, accessing the characters in reverse and accessing every other character. A couple of nasm macros are used to output the results to `stdout`. – David C. Rankin May 05 '22 at 17:04

0 Answers0