News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Stack alignment problem in Linux

Started by aw27, December 20, 2017, 08:28:17 PM

Previous topic - Next topic

aw27

Quote from: GoneFishing on December 21, 2017, 03:01:09 AM
Perhaps. After making use of argc, argv etc. if we need it
Did you already try main args ?
I have not tried anything, these are the ABI rules. The caller is expected to call with stack aligned no matter how many arguments are pushed on the stack. This applies both to Windowz  :biggrin: and SystemV.

GoneFishing

I'm not against ABI . I simply wanted to show you that rsp at main entry contains meaningful info.

aw27

Quote from: GoneFishing on December 21, 2017, 03:29:25 AM
I'm not against ABI . I simply wanted to show you that rsp at main entry contains meaningful info.
May not contain anything interesting because pointers to argc and argv come in rdi and rsi.

GoneFishing

Try this:

...
.code     
     main: 
       and rsp, -16   
       invoke printf,CStr("argc = %d ",10), [rdi] ; [rsp]
...

EDIT: forgot square brackets, corrected

aw27

It makes sense because rdi will be used for the first argument. Assemblers are low-level tools, we need to know how things happen.

GoneFishing

I agree.
It makes sense how to work with labels as entry point .  Now what if you want to pass real argc and argv to gtk_init not just nulls.
How would you do it?

aw27

Quote from: GoneFishing on December 21, 2017, 04:24:02 AM
I agree.
It makes sense how to work with labels as entry point .  Now what if you want to pass real argc and argv to gtk_init not just nulls.
How would you do it?
I don't know much about GTK, but I don't really see where your problem stands as far as ASM is concerned.
For documentation about GTK better you have a look at the Gnome website. GTK has a huge amount of functions. I also became aware that some of the functions I used in my example are deprecated and I have even prototyped the ones that replace them but ended not using them because it was out of scope.

GoneFishing

gtk_init takes 2 parameters: argc and argv
In the gtk1 example nullPtr's were used . That's why I asked you . I know about online GNOME docs I perfectly can use it.
Well , let's stop for today. Thanks for your time and efforts

aw27

Quote from: GoneFishing on December 21, 2017, 05:10:11 AM
gtk_init takes 2 parameters: argc and argv
In the gtk1 example nullPtr's were used . That's why I asked you . I know about online GNOME docs I perfectly can use it.
Well , let's stop for today. Thanks for your time and efforts
I understand your point, you don't believe that the arguments passed from the command line will reach your ASM program. They will.



public main

puts  proto :PTR
exit  proto :DWORD
printf proto :PTR, :VARARG
strlen proto :PTR

.data
argcount db "count %d",10,0
passedargs db "%s",10,0


.code

main:   
push r12
push r13
push r14
        and rsp, -16
mov r13d, edi
        mov r14, [rsi]

invoke printf, offset argcount, r13d
xor r12, r12
.while r12<r13
invoke printf, offset passedargs, r14
invoke strlen, r14
add r14,rax
inc r14
inc r12
.endw

pop r14
pop r13
pop r12
        invoke exit, 0

end main




GoneFishing

I was not sure how to do it technically correct in different variations.
Today we've worked out  first variant  main entry as label
Other possible variants are:
- entry main as proc without args - easy one
- entry main as proc with args       - needs to be discussed

BTW do we  really need  to push all those registers on main entry?

aw27

Quote from: GoneFishing on December 21, 2017, 06:51:22 AM
BTW do we  really need  to push all those registers on main entry?
I don't think so, but have not seen it documented either for Windows or for Linux.