News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

OPTION STACKBASE also broken in x64

Started by aw27, March 21, 2017, 07:58:08 PM

Previous topic - Next topic

TWell

#45
Just for fun :biggrin:
option casemap:none
;option frame:auto
;OPTION STACKBASE:RSP
;option win64:11

.code

sub1 proc dest:ptr, src:ptr, val1 : qword, val2:qword
mov dest, rcx
mov src, rdx
mov val1, r8
mov val2, r9
mov rax, qword ptr [rdx]
add rax, val1
add rax, val2
mov qword ptr [rcx], rax

ret
sub1 endp

end
ml64.exesub1    PROC
        push    rbp                                     ; 0000 _ 55
        mov     rbp, rsp                                ; 0001 _ 48: 8B. EC
        mov     qword ptr [rbp+10H], rcx                ; 0004 _ 48: 89. 4D, 10
        mov     qword ptr [rbp+18H], rdx                ; 0008 _ 48: 89. 55, 18
        mov     qword ptr [rbp+20H], r8                 ; 000C _ 4C: 89. 45, 20
        mov     qword ptr [rbp+28H], r9                 ; 0010 _ 4C: 89. 4D, 28
        mov     rax, qword ptr [rdx]                    ; 0014 _ 48: 8B. 02
        add     rax, qword ptr [rbp+20H]                ; 0017 _ 48: 03. 45, 20
        add     rax, qword ptr [rbp+28H]                ; 001B _ 48: 03. 45, 28
        mov     qword ptr [rcx], rax                    ; 001F _ 48: 89. 01
        leave                                           ; 0022 _ C9
        ret                                             ; 0023 _ C3
sub1    ENDP
HJWasm64.exesub1    PROC
        push    rbp                                     ; 0000 _ 55
        mov     rbp, rsp                                ; 0001 _ 48: 8B. EC
        mov     qword ptr [rbp+10H], rcx                ; 0004 _ 48: 89. 4D, 10
        mov     qword ptr [rbp+18H], rdx                ; 0008 _ 48: 89. 55, 18
        mov     qword ptr [rbp+20H], r8                 ; 000C _ 4C: 89. 45, 20
        mov     qword ptr [rbp+28H], r9                 ; 0010 _ 4C: 89. 4D, 28
        mov     rax, qword ptr [rdx]                    ; 0014 _ 48: 8B. 02
        add     rax, qword ptr [rbp+20H]                ; 0017 _ 48: 03. 45, 20
        add     rax, qword ptr [rbp+28H]                ; 001B _ 48: 03. 45, 28
        mov     qword ptr [rcx], rax                    ; 001F _ 48: 89. 01
        leave                                           ; 0022 _ C9
        ret                                             ; 0023 _ C3
sub1    ENDP

option casemap:none
option frame:auto
OPTION STACKBASE:RSP
option win64:11

.code

sub1 proc frame dest:ptr, src:ptr, val1 : qword, val2:qword
; mov dest, rcx
; mov src, rdx
; mov val1, r8
; mov val2, r9
mov rax, qword ptr [rdx]
add rax, val1
add rax, val2
mov qword ptr [rcx], rax

ret
sub1 endp

end
HJWasm64.exesub1    PROC
        mov     qword ptr [rsp+18H], r8                 ; 0000 _ 4C: 89. 44 24, 18
        mov     qword ptr [rsp+20H], r9                 ; 0005 _ 4C: 89. 4C 24, 20
        mov     rax, qword ptr [rdx]                    ; 000A _ 48: 8B. 02
        add     rax, qword ptr [rsp+18H]                ; 000D _ 48: 03. 44 24, 18
        add     rax, qword ptr [rsp+20H]                ; 0012 _ 48: 03. 44 24, 20
        mov     qword ptr [rcx], rax                    ; 0017 _ 48: 89. 01
        ret                                             ; 001A _ C3
sub1    ENDP


In Cvoid sub1C(long long *dest, long long *scr, long long val1, long long val2)
{
register long long val3 = val1;
val3 += val2;
*dest = val3;
}
cl.exesub1C   LABEL NEAR
        mov     qword ptr [rsp+20H], r9                 ; 0000 _ 4C: 89. 4C 24, 20
        mov     qword ptr [rsp+18H], r8                 ; 0005 _ 4C: 89. 44 24, 18
        mov     qword ptr [rsp+10H], rdx                ; 000A _ 48: 89. 54 24, 10
        mov     qword ptr [rsp+8H], rcx                 ; 000F _ 48: 89. 4C 24, 08
        sub     rsp, 24                                 ; 0014 _ 48: 83. EC, 18
        mov     rax, qword ptr [rsp+30H]                ; 0018 _ 48: 8B. 44 24, 30
        mov     qword ptr [rsp], rax                    ; 001D _ 48: 89. 04 24
        mov     rax, qword ptr [rsp+38H]                ; 0021 _ 48: 8B. 44 24, 38
        mov     rcx, qword ptr [rsp]                    ; 0026 _ 48: 8B. 0C 24
        add     rcx, rax                                ; 002A _ 48: 03. C8
        mov     rax, rcx                                ; 002D _ 48: 8B. C1
        mov     qword ptr [rsp], rax                    ; 0030 _ 48: 89. 04 24
        mov     rax, qword ptr [rsp+20H]                ; 0034 _ 48: 8B. 44 24, 20
        mov     rcx, qword ptr [rsp]                    ; 0039 _ 48: 8B. 0C 24
        mov     qword ptr [rax], rcx                    ; 003D _ 48: 89. 08
        add     rsp, 24                                 ; 0040 _ 48: 83. C4, 18
        ret                                             ; 0044 _ C3
cl.exe -Oxsub1C   PROC
        lea     rax, ptr [r8+r9]                        ; 0000 _ 4B: 8D. 04 08
        mov     qword ptr [rcx], rax                    ; 0004 _ 48: 89. 01
        ret                                             ; 0007 _ C3
sub1C   ENDP
pocc.exesub1C   PROC
        mov     qword ptr [rsp+8H], rcx                 ; 0000 _ 48: 89. 4C 24, 08
        mov     qword ptr [rsp+18H], r8                 ; 0005 _ 4C: 89. 44 24, 18
        mov     qword ptr [rsp+20H], r9                 ; 000A _ 4C: 89. 4C 24, 20
        mov     rax, qword ptr [rsp+18H]                ; 000F _ 48: 8B. 44 24, 18
        add     rax, qword ptr [rsp+20H]                ; 0014 _ 48: 03. 44 24, 20
        mov     rdx, qword ptr [rsp+8H]                 ; 0019 _ 48: 8B. 54 24, 08
        mov     qword ptr [rdx], rax                    ; 001E _ 48: 89. 02
        ret                                             ; 0021 _ C3
sub1C   ENDP
pocc.exe -Otsub1C   PROC
        add     r8, r9                                  ; 0000 _ 4D: 01. C8
        mov     qword ptr [rcx], r8                     ; 0003 _ 4C: 89. 01
        ret                                             ; 0006 _ C3
sub1C   ENDP


option casemap:none
option frame:auto
OPTION STACKBASE:RSP
option win64:11

sub1 proto dest:ptr, src:ptr, val1 : qword, val2:qword
.code

getSum proc public frame dest:ptr, src:ptr, val1 : qword, val2:qword
INVOKE sub1, dest, rdx, r8, r9
ret
getSum endp
end
HJWasm64.exegetSum  PROC
        mov     qword ptr [rsp+8H], rcx                 ; 0000 _ 48: 89. 4C 24, 08
        sub     rsp, 40                                 ; 0005 _ 48: 83. EC, 28
        mov     rcx, qword ptr [rsp+30H]                ; 0009 _ 48: 8B. 4C 24, 30
        call    sub1                                    ; 000E _ E8, 00000000(rel)
        add     rsp, 40                                 ; 0013 _ 48: 83. C4, 28
        ret                                             ; 0017 _ C3
getSum  ENDP


int sub1(long long *dest, long long *scr, long long val1, long long val2);
int getSumC(long long *dest, long long *scr, long long val1, long long val2)
{
return sub1(dest,scr,val1,val2);
}
cl.exegetSumC LABEL NEAR
        mov     qword ptr [rsp+20H], r9                 ; 0000 _ 4C: 89. 4C 24, 20
        mov     qword ptr [rsp+18H], r8                 ; 0005 _ 4C: 89. 44 24, 18
        mov     qword ptr [rsp+10H], rdx                ; 000A _ 48: 89. 54 24, 10
        mov     qword ptr [rsp+8H], rcx                 ; 000F _ 48: 89. 4C 24, 08
        sub     rsp, 40                                 ; 0014 _ 48: 83. EC, 28
        mov     r9, qword ptr [rsp+48H]                 ; 0018 _ 4C: 8B. 4C 24, 48
        mov     r8, qword ptr [rsp+40H]                 ; 001D _ 4C: 8B. 44 24, 40
        mov     rdx, qword ptr [rsp+38H]                ; 0022 _ 48: 8B. 54 24, 38
        mov     rcx, qword ptr [rsp+30H]                ; 0027 _ 48: 8B. 4C 24, 30
        call    sub1                                    ; 002C _ E8, 00000000(rel)
        add     rsp, 40                                 ; 0031 _ 48: 83. C4, 28
        ret                                             ; 0035 _ C3
cl.exe -OxgetSumC PROC
        jmp     sub1                                    ; 0000 _ E9, 00000000(rel)
getSumC ENDP

johnsa

Quote from: aw27 on March 24, 2017, 03:32:03 PM
Quote from: johnsa on March 23, 2017, 10:00:22 PM
After investigating this one I can confirm that Visual (C++) is NOT aligning the stack to 16.
Of course not and does not have to. What is required is to align the stack "before" the call is made. How is it possible to have simultaneously the stack aligned before the call and immediately after the call?
I give up. It is too much nonsense for me.

Perhaps read all the posts before replying to one previously ? :)
It was simply a message stating the current point in the investigation. The following message described that using the FRAME option on the proc ensures the alignment in both cases and adjusts RSP accordingly.
So you get sub rsp,28h instead of sub rsp,20h.

aw27

Quote from: TWell on March 24, 2017, 08:39:55 PM
getSumC PROC
        jmp     sub1                                    ; 0000 _ E9, 00000000(rel)
getSumC ENDP

Here is the proof that these High-level compilers are artificial intelligence driven.  :greenclp:

habran

Hi TWell :biggrin:
Thanks for your support!
I your last example you are showing a so called the Tail Call Elimination
John and myself have discussed it some time ago and concluded that it is not smart to use jump from the function, it can cause a hard to find bug, in our opinion it is much safer to use a call.
Cod-Father