flat assembler
Message board for the users of flat assembler.

Index > Linux > [Solved] Command Line arguments ordering?

Author
Thread Post new topic Reply to topic
system error



Joined: 01 Sep 2013
Posts: 671
system error
I was told that first argument is at RBP+16 and so on in multiply of 8 for 64-bit linux comment line arguments.

So the first argument should be [rbp+16] and the second one is at [rbp+24]. The program below works;
Code:
;./fasm chbase.asm
;./chbase 10110001b 16 ;change to base-16
;B1
format elf64 executable
entry begin

segment writeable
errmsg  db 'usage: chbase <value> <base>',0
numb    dq 0
base    dq 0
        
segment executable      
begin:  
        mov     rbp,rsp                 
        mov     rbx,[rbp]               
        dec     rbx                             
        cmp     rbx,2
        jne     .err
        
        mov     rdi,[rbp+24]    ;Argument 2
        call    atoi
        mov     [base],rax
        
        mov     rdi,[rbp+16]    ;Argument 1
        call    atoi
        mov     [numb],rax
                                                                        
        mov     rsi,[base]      ;Convert & Display
        mov     rdi,[numb]
        call    convert_base
        jmp     .done

.err:
        mov     rdi,errmsg
        call    strz_out

.done:  
        call    linefeed
        call    exit
;other functions follow...
    


The problem arises when I put the codes for Argument 1 before Argument 2's.

Code:
        mov     rdi,[rbp+16]    ;Argument 1
        call    atoi
        mov     [numb],rax
        
        mov     rdi,[rbp+24]    ;Argument 2
        call    atoi
        mov     [base],rax    


It produces funny result. This is confusing since the program should work regardless of the order I picked up the arguments. The problem persists even if I put those arguments in other registers before calling any of the functions. I can assure you that other functions are working just fine. atoi is my own function, not C.

So what seems to be the problem? I tried preserving RBP but to no avail.
Post 21 Dec 2014, 15:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Show all the associated code. If you leave stuff out we can't be sure what is happening.
Post 21 Dec 2014, 15:37
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
BTW, atoi is for null-terminated string conversion. So there should be no problem converting integers from the arguments since they are all null-terminated anyway.

One more thing, I tried adding a third argument and at first it works regardless of the order they appear in the source. But then (I don't know how) it's back to the old problem. I smell inconsistency here.
Post 21 Dec 2014, 15:42
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
revo... here's part of atoi:

Code:
atoi:
        push    rcx 
        push    rbx 
        push    r8 
        push    r9 
        push    rdx
        push    r15
        ...
        ...
        syscall
.out:
        mov     rax,r9
        pop     r15
        pop     rdx
        pop     r9 
        pop     r8 
        pop     rbx
        pop     rcx 
        ret    


There's just one syscall in there and nothing else. Its a leaf. It takes RDI as argument and returns RAX. That's about it.
Post 21 Dec 2014, 15:48
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 671
system error
nvm, I figured it out myself. stupid mistake Razz
thanks anyway revo
Post 21 Dec 2014, 17:05
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.