Author Topic: ALIGN 64  (Read 3070 times)

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: ALIGN 64
« Reply #15 on: August 30, 2015, 05:50:17 AM »
Come to think of it you actually HAVE to align all objects (at least the ones you use the macro in) to 64 from the top for this to work.

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: ALIGN 64
« Reply #16 on: August 30, 2015, 06:53:40 AM »
What happened with version 6.15, how did it fail?

It inserts zeros:
Code: [Select]
00401013                  ³.  90                     nop
00401014                  ³.  0000                   add [eax], al
00401016                  ³.  0000                   add [eax], al

Strange, now that I tested it again it fails with all assemblers, with an exception. Copied and pasted from your code above...
Code: [Select]
alignx macro x
if (($ - _TEXT) and (x - 1))
    org $ + x - (($ - _TEXT) and (x - 1))
endif
endm

What do you see in the debugger with this code?
Code: [Select]
.code
start:
 nops 19 ; misalign for testing
 int 3
codeStart:
  alignx 64
codeEnd:

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: ALIGN 64
« Reply #17 on: August 30, 2015, 07:38:17 AM »
It inserts zeros:

Yes, they all should pad with zeros.

Quote
Strange, now that I tested it again it fails with all assemblers, with an exception.

As it should if you fall trough the ORG offset as you do. You have to pad with nop's in that case.

Quote
What do you see in the debugger with this code?

I didn't test real code, only viewed the list output. Keep in mind that the $ offset is zero based so you cant use this approach in a library or object module.

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: ALIGN 64
« Reply #18 on: August 30, 2015, 10:45:03 AM »
Keep in mind that the $ offset is zero based so you cant use this approach in a library or object module.

Very good point :t

I've included Align64 in the latest MasmBasic edition of 30 August. It will throw an error when used in a library:

Code: [Select]
include \masm32\include\masm32rt.inc
AlignX macro abytes
Local xbytes
  ifndef start
.err <### AlignX can't be used in libraries and object modules ###>
  endif
  xbytes=abytes-(($-_TEXT) and (abytes-1))
  if xbytes
db xbytes dup(90h)
  endif
endm
Align64 equ <AlignX 64>

.code
start:
 nops 20 ; misalign for testing
codeStart:
  Align64
codeEnd:

  mov ebx, offset start
  print hex$(ebx), " is start:", 13, 10
  mov ebx, offset codeStart
  print hex$(ebx), " is codeStart:", 13, 10
  mov ebx, offset codeEnd
  print hex$(ebx), " is codeEnd:", 13, 10
  mov eax, ebx
  sub eax, codeStart
  print str$(eax), " bytes inserted"
  exit
end start

Output:
Code: [Select]
00401000 is start:
00401014 is codeStart:
00401040 is codeEnd:
44 bytes inserted