0

So i was experimenting with printing out arguments that were passed in terminal.

%include "OPCODES.asm"
%include "FILE_DESCRIPTORS.asm"
%include "FUNCTIONS.asm"

SECTION .data
    noMsg db "No Arguments Passed!",0
    foundMsg db "Argument Found!",0
SECTION .bss
SECTION .text
global _start
_start:

    POP RCX
    CMP RCX , 1
    JE noArgs
    
    printArgs:
        CMP RCX , 1
        JE finished
        printLn foundMsg
        DEC RCX
        JMP printArgs

noArgs:
    printLn noMsg
    JMP finished
    
finished:
    exit

the part where i check whether there are no arguments works , but when there is at least one argument passed it goes in an unlimited loop, and i don't really know why, it decrements the counter and checks whether it is 1.

"exit" & "printLn" are macros defined in FUNCTIONS.asm .

Im working on linux kubuntu and i use NASM for assembly.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Mr.E
  • 1
  • 3
  • Does `printLn` preserve RCX? I'm guessing not; single step with a debugger to see. (If that's the problem, use a call-preserved register like RBX instead). Where did you get FUNCTIONS.asm? It's not a standard part of NASM. – Peter Cordes Feb 24 '21 at 06:23
  • Also note that if your program was started by `execve("your_prog", [NULL], envp)`, your argc will be 0, not 1, so it would be better to use `jbe noArgs` instead of printing "arg found" 2^64 times in that case. – Peter Cordes Feb 24 '21 at 06:27
  • [What registers are preserved through a linux x86-64 function call](https://stackoverflow.com/q/18024672) / [What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64](https://stackoverflow.com/q/2535989) might be relevant, except that `printLn` isn't a function or a system call; it's a macro that has no reason to follow any standard "calling convention". (Although it must make a system call, so clobbering RCX would be normal if it doesn't specifically preserve it.) – Peter Cordes Feb 24 '21 at 06:31
  • @PeterCordes Thanks for the help ! i changed RCX to RBX and everything went smooth. also FUNCTION.asm is just a file i made to store my macros like printLn , exit , etc. – Mr.E Feb 24 '21 at 07:14
  • Oh, so you wrote `printLn` yourself? Not an exact duplicate, but having your own code overwrite your own registers is something you should generally notice with a debugger when single-stepping. – Peter Cordes Feb 24 '21 at 07:17

0 Answers0