Author Topic: Static RSP built in JWasm  (Read 34091 times)

japheth

  • Guest
Re: Static RSP built in JWasm
« Reply #30 on: July 08, 2013, 01:24:17 PM »
because that is what ML64 emits, it has obviously abandoned S_BPREL32

I wondered why ML64 has abandoned S_BPREL32; after a bit of testing it turned out that it apparently wasn't just the joy to do things differently. S_BPREL32 works correctly for register EBP only - that is, if the upper 32-bits of RBP aren't zero, S_BPREL32 isn't appropriate.

It's a bug, but usually it doesn't matter, because even if the base address of your 64-bit binary is beyond the 4 GB frontier, the stack will still reside in the first 4 GB.

Here's a test case:
Code: [Select]
ExitProcess proto :dword

includelib <kernel32.lib>

.data?

stack db 8000h dup (?)
eos  dq 4 dup (?)

.code

option frame:auto
option win64:3

p2 proc frame a1:dword, a2:qword
local l1:qword
local l2:qword
mov rax,123
mov l1, rax
mov rcx,234
mov l2, rcx
ret
p2 endp

start proc frame
mov rsp, offset eos
invoke p2, 1, 2
invoke ExitProcess, 0
start endp

end start

assemble: jwasm -Zi -win64 test.asm
link: link /debug /base:0x140000000 test.obj /libpath:\wininc\lib64

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #31 on: July 08, 2013, 07:15:36 PM »
Quote
It's a bug, but usually it doesn't matter, because even if the base address of your 64-bit binary is beyond the 4 GB frontier, the stack will still reside in the first 4 GB.
does it mean that we need to us RSP for a greater stack allocation?
is it an Intel bug?
Cod-Father

japheth

  • Guest
Re: Static RSP built in JWasm
« Reply #32 on: July 09, 2013, 02:25:07 PM »
does it mean that we need to us RSP for a greater stack allocation?

Not at all. It just means what I already did say: that due to Windows current 64-bit implementation details the upper 32 bits or RSP ( and RBP, if it's used as frame pointer ), is zero - and that's why S_BPREL32 works "by chance".

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #33 on: July 09, 2013, 03:01:25 PM »
so, if I understood correctly,  in any case we can not allocate stack more than 4 GB?
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #34 on: July 13, 2013, 06:41:56 PM »
I have fixed one bug and improved saving of xmm registers

before ve had this:
Code: [Select]
00000000000D1176  mov         qword ptr [rsp+8],rcx 
00000000000D117B  mov         qword ptr [rsp+10h],rdx 
00000000000D1180  sub         rsp,38h                        ;here we subtract rsp for locals xmm regs
00000000000D1184  movdqa      xmmword ptr [rsp],xmm1 
00000000000D1189  movdqa      xmmword ptr [rsp+10h],xmm2 
00000000000D118F  movdqa      xmmword ptr [aVar],xmm3 
00000000000D1195  sub         rsp,30h                       ;here we subtract rsp again for locals and shadows
00000000000D1199  mov         eax,dword ptr [val2] 
00000000000D119D  mov         dword ptr [bVar],eax 
00000000000D11A1  mov         qword ptr [val],21h 
00000000000D11AA  mov         rdx,qword ptr [val] 
00000000000D11AF  mov         rcx,0D4008h 
00000000000D11B9  call        printf (0D1292h) 
00000000000D11BE  mov         rax,22h   
00000000000D11C5  mov         qword ptr [aVar],rax 
00000000000D11CA  mov         rdx,qword ptr [aVar] 
00000000000D11CF  mov         rcx,0D400Fh 
00000000000D11D9  call        printf (0D1292h)   
00000000000D11DE  call        testproc2 (0D11FAh)   
00000000000D11E3  movdqa      xmm1,xmmword ptr [rsp+40h]  ;wrong displacement shpuld be 30h
00000000000D11E9  movdqa      xmm2,xmmword ptr [rsp+50h]  ;wrong displacement shpuld be 40h
00000000000D11EF  movdqa      xmm3,xmmword ptr [rsp+60h]  ;wrong displacement shpuld be 50h
00000000000D11F5  add         rsp,68h 
00000000000D11F9  ret   

after fix:
Code: [Select]
0000000000DE1176  mov         qword ptr [rsp+8],rcx 
0000000000DE117B  mov         qword ptr [rsp+10h],rdx 
0000000000DE1180  sub         rsp,68h                ;here we subtract at ones space for xmm and locals
0000000000DE1184  movdqa      xmmword ptr [rsp+30h],xmm1 
0000000000DE118A  movdqa      xmmword ptr [rsp+40h],xmm2 
0000000000DE1190  movdqa      xmmword ptr [rsp+50h],xmm3 
0000000000DE1196  mov         eax,dword ptr [val2] 
0000000000DE119A  mov         dword ptr [bVar],eax 
0000000000DE119E  mov         qword ptr [val],21h 
0000000000DE11A7  mov         rdx,qword ptr [val] 
0000000000DE11AC  mov         rcx,0DE4008h 
0000000000DE11B6  call        printf (0DE1292h) 
0000000000DE11BB  mov         rax,22h   
0000000000DE11C2  mov         qword ptr [aVar],rax 
0000000000DE11C7  mov         rdx,qword ptr [aVar] 
0000000000DE11CC  mov         rcx,0DE400Fh 
0000000000DE11D6  call        printf (0DE1292h)   
0000000000DE11DB  call        testproc2 (0DE11F7h)       
0000000000DE11E0  movdqa      xmm1,xmmword ptr [rsp+30h]  ;now location is correct
0000000000DE11E6  movdqa      xmm2,xmmword ptr [rsp+40h] 
0000000000DE11EC  movdqa      xmm3,xmmword ptr [rsp+50h] 
0000000000DE11F2  add         rsp,68h 
0000000000DE11F6  ret

Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #35 on: July 13, 2013, 07:00:31 PM »
I have find out that debugging depends on the linker
if you build with MSVC8 you can debug it with MSVC8 Debugger or WinDbg 6.12
If you build it with MSVC12 you can debug it with MSVC12 Debugger or WinDbg 6.2
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #36 on: July 17, 2013, 06:43:57 AM »
sorry, mea culpa :icon_redface:
the last bug eliminated(hopefully) :bgrin:
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #37 on: July 18, 2013, 09:40:21 AM »
the last bug was not really the last, it just pretended to be the one :icon_eek:
but this one was real one, just undercover :biggrin:
now everything glides :t
Cod-Father

japheth

  • Guest
Re: Static RSP built in JWasm
« Reply #38 on: July 18, 2013, 10:54:49 PM »
but this one was real one, just undercover :biggrin:

Wow, great!

I always wonder, when releasing another jwasm version with a few dozen bug fixes, how any of the previous versions could ever have been regarded as "stable".  :bgrin:

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #39 on: July 18, 2013, 11:53:14 PM »
we realists always hope for best and at the same tame expect the worst :biggrin:
I experience now the responsibility which a programmers have by publishing their code :icon_mrgreen:
but at the same time I am happy to be able to create that perfect tool  8)
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #40 on: July 19, 2013, 08:58:55 PM »
one more small fix for debug info :biggrin:
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #41 on: July 21, 2013, 09:56:12 AM »
I have replaced the folder with the better one ::)
If you are curious why am I insisting on this version of jwasm when Japheth published new version
JWasm211, which does include option STACKBASE, the answer is:
1) JWasm211 doesn't align first local to 16 byte
2) JWasm211 doesn't use home space if it is free to store registers
3) JWasm211 doesn't have .for/.endfor

I appreciate Japheth and his precious JWasm, I just added little bit more to it :t   
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #42 on: July 30, 2013, 12:07:02 PM »
I uploaded at the top a new, better version of JWasm.exe with some more fixes
as far as I tested it, now debug info works in all situations

I waned to upload complete source with MSVC12 project and exe but it doesnt fit in allowed forum size
even without exe it is not small enough when compressed with winzip
so, I will attach here main folder with .c extension and in new post H folder
I think that the upload size should be increased to 1 MB :biggrin:
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #43 on: July 30, 2013, 12:09:56 PM »
here are headers, just decompress it and drop it in JWasm folder
Cod-Father

habran

  • Member
  • *****
  • Posts: 1225
    • uasm
Re: Static RSP built in JWasm
« Reply #44 on: August 17, 2013, 07:19:23 PM »
Hi again :biggrin:

I have worked more on JWasm and added some more sophisticated features to it
Now it can decide by itself if there is a need for reserved stack
if function is not having invoke inside, there is no need for alocating the stack space
also, if there is USES command and a space for up to 4 registers in the home space
it will PUSH the last one for alignment instead SUB RSP,8

all together it makes it intelligent tool
you don't need any more use:
option win64:0
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

it will realize that there is no need for  PROLOGUE

the binaries are at the top of this thread

it is now advanced so much that Germans would call it "Vorsprung durch Technik" ;)

here is the .c source
Cod-Father