Terraspace / UASM

UASM - Macro Assembler
http://www.terraspace.co.uk/uasm.html
Other
220 stars 49 forks source link

-Sg option produces incorrect listing file #117

Closed db4 closed 2 years ago

db4 commented 4 years ago

Consider the following test.asm:

.CODE

option WIN64: 1

test_f PROC
     ret
test_f ENDP

END

Assemble it with uasm -win64 -Fl -Sg test.asm (UASM is built from the latest master branch commit). The following .lst file is created:

00000000                        test_f PROC
00000000  4883EC08          *   db 48h
0000                            push rbp
                                     reg rbp
000000000000004                     rbp, rsp
   00000004  4883C408           .setframe rbp, 00000008  C3                   *   .endprolog
00000005                             ret
00000005                    *   mov rsp, rbp
00000008                    *   pop rbp
00000009                    *   retn

This is totally wrong starting from db 48h. The correct code (disassembled) is:

test_f:
  0000000000000000: 48 83 EC 08        sub         rsp,8
  0000000000000004: 48 83 C4 08        add         rsp,8
  0000000000000008: C3                 ret
john-terraspace commented 4 years ago

Hi,

The 48h is a Windows ABI feature (the rex prefix) is added in the prologue to conform to other MS Compiler tools which can detect prologue based on the presence of the rex prefix. It has no effect.

With win64:1 RBP is being used as the stack base and there no attempt is made to optimise the prologue/epilogue. With other modes it would see there are no params and no locals and no calls so it would consider it a leaf function and optimise out the prologue/epilogue completely. The push rbp aligns the stacks so this is as expected for that mode.

From: Dmitry Bely notifications@github.com Sent: 06 November 2019 06:30 To: Terraspace/UASM UASM@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [Terraspace/UASM] -Sg option produces incorrect listing file (#117)

Consider the following test.asm:

.CODE

option WIN64: 1

test_f PROC ret test_f ENDP

END

Assemble it with uasm -win64 -Fl -Sg test.asm (UASM is built from the latest master branch commit). The following .lst file is created:

00000000 test_f PROC 00000000 4883EC08 db 48h 0000 push rbp reg rbp 000000000000004 rbp, rsp 00000004 4883C408 .setframe rbp, 00000008 C3 .endprolog 00000005 ret 00000005 mov rsp, rbp 00000008 pop rbp 00000009 * retn

This is totally wrong starting from db 48h. The correct code (disassembled) is:

test_f: 0000000000000000: 48 83 EC 08 sub rsp,8 0000000000000004: 48 83 C4 08 add rsp,8 0000000000000008: C3 ret

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Terraspace/UASM/issues/117?email_source=notifications&email_token=AEAZAVCWPJRCDZV2VASRX3TQSJP7XA5CNFSM4JJQJUCKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HXEEPSA , or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAZAVA2HZTZH2MZCCXUS73QSJP7XANCNFSM4JJQJUCA .

db4 commented 4 years ago

The 48h is a Windows ABI feature (the rex prefix) is added in the prologue to conform to other MS Compiler tools which can detect prologue based on the presence of the rex prefix. It has no effect.

Yes, but listing contains neither sub rsp,8 nor add rsp,8 that is actually generated. Instead, it shows

00000005                    *   mov rsp, rbp
00000008                    *   pop rbp

that are NOT generated. Why?

john-terraspace commented 4 years ago

I see, the listing doesn’t correspond. I will have a look.

From: Dmitry Bely notifications@github.com Sent: 06 November 2019 09:15 To: Terraspace/UASM UASM@noreply.github.com Cc: John Hankinson john@terraspace.co.uk; Comment comment@noreply.github.com Subject: Re: [Terraspace/UASM] -Sg option produces incorrect listing file (#117)

The 48h is a Windows ABI feature (the rex prefix) is added in the prologue to conform to other MS Compiler tools which can detect prologue based on the presence of the rex prefix. It has no effect.

Yes, but listing contains neither sub rsp,8 nor add rsp,8 that is actually generated. Instead, it shows

00000005 mov rsp, rbp 00000008 pop rbp

that are NOT generated. Why?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Terraspace/UASM/issues/117?email_source=notifications&email_token=AEAZAVBUGLUJRCIQNOBOFW3QSKDKJA5CNFSM4JJQJUCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDF2VNA#issuecomment-550218420 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAZAVGGVBFDJSKLQZPX533QSKDKJANCNFSM4JJQJUCA .

john-terraspace commented 4 years ago

Found a reason for this, the listing is written on the first pass, and only on the second pass does the assembler have enough information to implement FPO. So the listing is based on the unoptimised pass 1 generated code.