News for hackers. Stuff that matters.
Re:i386 Assembly Language trick for storing data in .text by higherhalf on Thursday November 09, 2023 @11:34AM (#38207185)
This can be done even simpler:

  global _start
  _start:
      jmp next
  string:
      db `Hello World!\n`
  len: equ $ - string
  next:
      mov ecx, string
      mov edx, len
      mov ebx, 1
      mov eax, 4
      int 80h
  
      mov ebx, 0
      mov eax, 1
      int 80h
For NASM, it can also be put into a macro, for example printing to video memory at 0xb8000:

  %macro print 1
      mov ecx, %%loop_start - %%strdata
      mov eax, 0x0700
      jmp %%loop_start
  %%strdata: db %1
  %%loop_start:
      mov al, [%%strdata + ecx - 1]
      mov [0xb8000 + ecx * 2 - 2], ax
      loop %%loop_start
  %endmacro
Re:i386 Assembly Language trick for storing data in .text by projektfu on Thursday November 09, 2023 @11:45AM (#38207352)
That will require a fixup or a fixed load address. The example in the article is position independent.
Re:i386 Assembly Language trick for storing data in .text by fargle on Thursday November 09, 2023 @11:50AM (#38207423)
the author wanted it to be position-independent (PIC), so it works no matter what address the .text segment is loaded to and run.

This example uses a fixed symbolic reference ("string:") and is the normal way to do it. The trick is to it in a PC relative way.