3

In NASM, I can easily do it using label like this

times 510-($-$$) db 0
                 db 0x55, 0xaa

However, I can not use label in a GAS .rept command, It is invalid

current:
.rept 510-current-_start
.byte 0
.endr
; Error occures `Error: bad or irreducible absolute expression`
  1. I wonder what is a absolute expression(I saw it in a gas document), I think 510-current-_start should be accepcted because the value of this is calculated while compiling.

  2. How can I elegantly put 0x55 and 0xaa in a right position?

  3. I found .space works but .rept doesn't, why?

; It works
current:
.space 510-current-_start

; Error: bad or irreducible absolute expression
current:
.rept 510-current-_start
.byte 0
.endr

EDITED:

It does't work too(using parentheses), I think It might be a bug?

; Error: bad or irreducible absolute expression
current:
.rept 510-(current-_start)
.byte 0
.endr
Markity
  • 193
  • 8
  • `.org 510` should work, Some options in the answers to this other SO question: https://stackoverflow.com/questions/47859273/calculating-padding-length-with-gas-att-directives-for-a-boot-sector – Michael Petch Dec 08 '22 at 03:48
  • You are right. `.org 510` works. And `.space 510-(.-_start)` also works. But the code below doesn't work? `.rept 510-(.-_start)\n .byte 0\n .endr` – Markity Dec 08 '22 at 04:20
  • @Markity: You don't really need the `.rept` with `gas`. If `.org` causes the file position to advance, the default action is to fill with `0`. – sj95126 Dec 08 '22 at 05:26
  • 1
    @Michael Petch Doesn't the boot loader need to be located at `0x7C00`? Or are `.org` directives relative in GAS, allowing you to say `.org 510` to evaluate to `.org 7E00h`? – puppydrum64 Dec 08 '22 at 14:18
  • 1
    @puppydrum64 : The `.org` directive doesn't work the same in GAS as it does in something like NASM. In GAS it is relative https://stackoverflow.com/questions/11085027/gas-org-different-from-org-in-nasm . With GAS you'd need to specify the origin point during the linking stage. – Michael Petch Dec 08 '22 at 14:24
  • @MichaelPetch That's what I imagined. Also I made a typo in my earlier comment, I meant `.org 7DFE` at the end there. – puppydrum64 Dec 08 '22 at 14:27
  • @Markity Did you try using parentheses? As in, `.rept 510-(current-_start)`? Maybe the order of operations you used is resulting in a negative number which obviously doesn't make sense. – puppydrum64 Dec 08 '22 at 14:29
  • @puppydrum64 I tryied but still failed, maybe `.rept` only accepct a specific number. I should ignore this problem because I can use other methods. – Markity Dec 09 '22 at 04:08

0 Answers0