Author Topic: Stack alignment problem in Linux  (Read 1590 times)

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #15 on: December 21, 2017, 03:08:35 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

  • Member
  • *****
  • Posts: 1056
  • Gone fishing
Re: Stack alignment problem in Linux
« Reply #16 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.

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #17 on: December 21, 2017, 03:34:39 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

  • Member
  • *****
  • Posts: 1056
  • Gone fishing
Re: Stack alignment problem in Linux
« Reply #18 on: December 21, 2017, 03:39:46 AM »
Try this:
Code: [Select]
...
.code     
     main: 
       and rsp, -16   
       invoke printf,CStr("argc = %d ",10), [rdi] ; [rsp]
...
EDIT: forgot square brackets, corrected

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #19 on: December 21, 2017, 03:58:21 AM »
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

  • Member
  • *****
  • Posts: 1056
  • Gone fishing
Re: Stack alignment problem in Linux
« Reply #20 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?

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #21 on: December 21, 2017, 04:52:55 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

  • Member
  • *****
  • Posts: 1056
  • Gone fishing
Re: Stack alignment problem in Linux
« Reply #22 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

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #23 on: December 21, 2017, 06:17:20 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.

Code: [Select]

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

  • Member
  • *****
  • Posts: 1056
  • Gone fishing
Re: Stack alignment problem in Linux
« Reply #24 on: December 21, 2017, 06:51:22 AM »
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?

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: Stack alignment problem in Linux
« Reply #25 on: December 21, 2017, 07:36:07 PM »
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.