Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:06:42+00:00 2026-06-11T07:06:42+00:00

Sometimes the correct argc value is returned, sometimes 0, and sometimes (seemingly)random numbers… all

  • 0

Sometimes the correct argc value is returned, sometimes 0, and sometimes (seemingly)random numbers… all from the same executable.

.section .text
.global _start
_start:
    movq    $1, %rax
    popq    %rdi
    syscall

For example:

%as -o this.o this.s ; ld -o this this.o

%./this; echo $?
1

%./this 1; echo $?
0

%./this 1 2; echo $?
3

%./this 1 2 a; echo $?
4

%./this 1 2 a f; echo $?
0

%_

I’m kind of new to assembly, but I was pretty confident that getting the argument count was as easy as popping it off the stack like in Linux, where the System V ABI documents that RSP points at argc in a freshly execve‘d process.

Am I doing something wrong, or is this just really messed up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:06:44+00:00Added an answer on June 11, 2026 at 7:06 am

    I was confused in the same case on FreeBSD 9.0/amd64. What I did is (I used nasm for assembler):

    $ cat foo.asm
    global _start
    _start:
            mov     rax, 4          ; write
            mov     rdi, 1          ; stdout
            mov     rsi, rsp        ; address
            mov     rdx, 16         ; 16bytes
            syscall
    
            mov     rax, 1          ; exit
            syscall
    $ nasm -f elf64 foo.asm && ld -o foo foo.o
    $ ./foo | hd
    00000000  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
    00000010
    $ ./foo 2 | hd
    00000000  02 00 00 00 00 00 00 00  b8 dc ff ff ff 7f 00 00  |................|
    00000010
    $ ./foo 2 3 | hd
    00000000  00 00 00 00 00 00 00 00  03 00 00 00 00 00 00 00  |................|
    00000010
    $ ./foo 2 3 4 | hd
    00000000  00 00 00 00 00 00 00 00  04 00 00 00 00 00 00 00  |................|
    00000010
    $ ./foo 2 3 4 5 | hd
    00000000  05 00 00 00 00 00 00 00  b0 dc ff ff ff 7f 00 00  |................|
    00000010
    

    I expected that argc was at rsp, but it was not.

    I guessed that the kernel (the image activator) sets registers. I searched the source tree, I found the following code in /usr/src/sys/amd64/amd64/machdep.c (exec_setregs).

            regs->tf_rsp = ((stack - 8) & ~0xFul) + 8;
            regs->tf_rdi = stack;           /* argv */
    

    EDIT: machdep.c has been split up and this function can now be found in exec_machdep.c

    These lines look saying that rsp is aligned, actual data are at rdi. I changed my code, and I got expected results.

    $ cat foo.asm
    global _start
    _start:
            push    rdi
            mov     rax, 4          ; write
            mov     rdi, 1          ; stdout
            pop     rsi
            mov     rdx, 16         ; 16bytes
            syscall
    
            mov     rax, 1          ; exit
            syscall
    $ nasm -f elf64 foo.asm && ld -o foo foo.o
    $ ./foo | hd
    00000000  01 00 00 00 00 00 00 00  b0 dc ff ff ff 7f 00 00  |................|
    00000010
    $ ./foo 2 | hd
    00000000  02 00 00 00 00 00 00 00  a8 dc ff ff ff 7f 00 00  |................|
    00000010
    $ ./foo 2 3 | hd
    00000000  03 00 00 00 00 00 00 00  a8 dc ff ff ff 7f 00 00  |................|
    00000010
    $ ./foo 2 3 4 | hd
    00000000  04 00 00 00 00 00 00 00  a8 dc ff ff ff 7f 00 00  |................|
    00000010
    $ ./foo 2 3 4 5 | hd
    00000000  05 00 00 00 00 00 00 00  a8 dc ff ff ff 7f 00 00  |................|
    00000010
    

    Can you try rdi?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

TCP and UDP(sometimes) use a simple checksum to make sure the contents are correct.
Sometimes i face the following problem : string txt = con.Request.Params[Par_name].ToString();//the original par value
Sometimes I try a customization/command in my vimrc. Everything seens to be correct, but
Sometimes I forget to use the return value when I have to. For example
I'm getting sometimes the error "Text node cannot appear in this state" in my
Sometimes my ASP.NET application returns a value of ::1 when querying Page.Request.UserHostAddress. I can't
We have some company middleware and sometimes its difficult to find the correct version
As I understand it (not that I'm correct) Drawables are generally correctly removed from
Have a class Hero. Sometimes I need a deep copy (when all members are
I create Grails project use STS IDE . And sometimes i see not correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.