flat assembler
Message board for the users of flat assembler.

Index > Main > Popping 7th function parameter off the stack on AMD64?

Author
Thread Post new topic Reply to topic
fpga



Joined: 22 Sep 2009
Posts: 36
fpga
The fasm function below is called by the c++ code underneath it.
It was ok until I added a 7nth parameter to both modules to work out
how to use the stack for function parameters. What am I doing wrong?

Code:
format ELF64 
section '.text' executable
public fn
fn:
   mov eax,edi
   add eax,esi
   add eax,edx
   add eax,ecx
   add eax,r8d    
   add eax,r9d
                         
   pop rdi        ;<=====!!! why's this an illegal instruction???

   add eax, edi
   ret
    


Code:
#include <iostream>
using namespace std;
//extern "C" int fn(int a,int b,int c, int d, int e, int f);
extern "C" int   fn(int a,int b,int c, int d, int e, int f, int g);
int main(){
   //cout << "params added = " << fn(1,2,3,4,5,6) << endl;
   cout << "params added = " <<   fn(1,2,3,4,5,6,7) << endl;
   return 0;
    
Post 05 Oct 2009, 18:54
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
I think you get the error in RET actually because with POP RDI you removed the return address from the stack.

I don't remember how the calling convention was but check with this:
Code:
; remove pop rdi
;replace add eax, edi with this:
  add eax, [rsp+8]    
Post 05 Oct 2009, 19:02
View user's profile Send private message Reply with quote
fpga



Joined: 22 Sep 2009
Posts: 36
fpga
You are spot on!!!
Thanks very much
I need to read up on this
Post 05 Oct 2009, 19:06
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Post 05 Oct 2009, 19:10
View user's profile Send private message Reply with quote
fpga



Joined: 22 Sep 2009
Posts: 36
fpga
I had no awareness
a) that the function return address was before the parameters on the stack,
b) that rsp is the stack pointer
c) or that +8 means +8 bytes ie the next 64 bit word on stack
Thank you
Post 05 Oct 2009, 19:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.