Author Topic: Command Line Arguments using NASM and GCC  (Read 21050 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Command Line Arguments using NASM and GCC
« on: February 17, 2018, 08:08:14 AM »
A simple, lightweight code to take command line input to calculate series of SIN values. Should be easy to convert to any trigonometric functions.
Code: [Select]
D:\NASM>sine

 Usage: sine v1 v2 vn

D:\NASM>sine 3.4
sin(3.4000) rad = -0.255541

D:\NASM>sine 3.4 0.0 -9.2 7.6 1.5
sin(3.4000) rad = -0.255541
sin(0.0000) rad = 0.000000
sin(-9.2000) rad = -0.222890
sin(7.6000) rad = 0.967920
sin(1.5000) rad = 0.997495

EDIT: Added 64-bit version (Win64)
« Last Edit: March 08, 2018, 06:45:47 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Command Line Arguments using NASM and GCC (win32)
« Reply #1 on: February 17, 2018, 11:36:49 AM »
Just added the 64-bit version for Win64

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Command Line Arguments using NASM and GCC for Linux
« Reply #2 on: February 21, 2018, 11:20:10 AM »
Added Linux versions (32-bit and 64-bit).

I don't know if I have / haven't reached my upload limit on this board (which is 5MB I think). If I have, then the attachments below could be my last token.

Edit: Added "cosr.asm" as an example for using "ld" instead of GCC for command-line arguments.

« Last Edit: March 08, 2018, 06:47:34 PM by stressful »

Offline rajex

  • New Member
  • Posts: 1
Re: Command Line Arguments using NASM and GCC
« Reply #3 on: March 07, 2018, 01:23:07 PM »
Can you help me understand how the OS calls the binary with the command-line arguments on the stack to the _start subroutine, I can't quite figure out my around esp and ebp.
« Last Edit: March 07, 2018, 01:24:45 PM by rajex »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Command Line Arguments using NASM and GCC
« Reply #4 on: March 07, 2018, 08:44:25 PM »
Hi rajex,
Welcome to the Forum.

In 32-bit code (at least) the "_start:" label is not "call"ed, so is not really a "subroutine". There is no return address on the stack, the first thing is "argc". (unless you've pushed ebp) Usually gcc is not involved in this case, so the "subject" may not be accurate.

My computer recently crashed and I didn't have my code backed up so I don't have an example at the moment. Can rewrite one, but not really in the mood, so see what you can do...

Best,
Frank


Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Command Line Arguments using NASM and GCC
« Reply #5 on: March 07, 2018, 10:28:35 PM »
Can you help me understand how the OS calls the binary with the command-line arguments on the stack to the _start subroutine, I can't quite figure out my around esp and ebp.

rajex

as told by Frank, this example is configured for GCC. _start is an "ld" thing and has a slightly different method of placing / taking the command line arguments.

Anyway, if memory serves, "ld" places argument count on top of stack and the rest are pointers to the subsequent arguments. One good thing about "ld" is that it separates the arguments with 0 for you so you can have null-terminated strings for each. Should be something like this;
Code: [Select]
section .text
_start:
mov ebp,esp

mov eax,[ebp] ;arg count
mov ebx,[ebp+4] ;prog's name string address
mov ecx,[ebp+8] ;arg1 string address
mov edx,[ebp+12] ;arg2 string address

Hope this helps.