Jump to content

how to pass bash variables

- - - - -

  • Please log in to reply
19 replies to this topic

#1
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Hey all, is there anyway I can pass bash variables to an assembly program, is the only way through C++

#2
Gunner

Gunner

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Nah, impossible :-P ... Actually fairly easy... You would have to parse the command line of your app when it starts. Right, you are talking about something like: myapp.exe foo > bar or whatever you want to pass to your app?

#3
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
I dnt kno what that means...I'm working in Linux and what is being redirected and to where? but I figured I can get bash create a file then get my assembly program to read it and then return results in file, then bash script will read file and delete the file after....that's the best thing I can think off, unless there is a cooler way to do it

#4
Gunner

Gunner

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
this is a command line: nasm -f elf myfile.asm

similar to how you assemble in linux right? nasm would be your exe and the parameters are -f elf and myfile.asm... nasm gets the commandline and parses it for the different parts.. I don't use linux but found this (google is your best friend programming) :-P Writing A Useful Program With NASM

#5
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
How do I access the values passed in command line ,inside my program....

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
main:
    push    ebp
    mov     ebp, esp
    sub     esp, 12             ; save space for 3 local int variables
    
    mov     ecx, [ebp + 8]      ; ECX = argc
    mov     edx, [ebp + 12]     ; EDX = argv
    mov     eax, [edx]          ; EAX = argv[0]
    mov     ebx, [edx + 4]      ; EBX = argv[1]
    
    ; ...your code here...
    
    add     esp, 12             ; deallocate local variables
    pop     ebp
    ret
This is just an example. The 12 bytes of local variables was arbitrary; you can have whatever you want there, or even nothing. Also note that this is 32-bit code.

Edited by dargueta, 27 July 2011 - 09:05 PM.
Fixed code

sudo rm -rf /

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US

Quote

mov ecx, [ebp + 4]

EBP+4? No return instruction pointer?

I mean, in Windows I use the RET instruction and it exits nicely.

And also, NASM accepts 0x* syntax, too, as well as *h.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Windows and Linux do things differently. For some reason ret segfaults on me some times and not others; I still haven't really managed to figure it out.

And yes, I'm aware NASM accepts the 0x syntax. I learned assembly language on an older assembler that only understood the *H syntax.
sudo rm -rf /

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
I just think 0x* looks better. Though I still have to use *h on MASM32.

How does Linux/Unix tell whether it's the next command line argument or not, when it makes that argv array?

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Typically it depends on what invoked the program. Different shells divide the arguments different ways. For example, with bash, a string in double quotes is considered a single argument. DOS, at least in the last version I used (XP I think) ignores the quotes, so something like this:

cd "C:\Documents and Settings"

Will get you an error along the lines of "directory not found." You have to do it the dinosaur way and type

cd C:\Docume~1

Basically what shells do is read in the command line, parse it their own way, turn it into a string array and pass it to execve(), execvp() or some similar function. I did something like this a while back for a CS class I was taking.
sudo rm -rf /

#11
untitled_1

untitled_1

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
I have two question:

1. so say i have a program called runner( this is the .exe ) i can then type

./runner 12 ( some random value 12 being passed to runner )

can i access the value 12 through the stack frame ?

2. is 12 passed as a chars, if thats the case i assume i have to create an integer variable

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Like in C/C++, everything is passed in as character strings (pointers to strings, actually) and you're expected to convert everything you need to the appropriate format. So you're either going to have to link with the standard C library and use strtol() or atoi(), or write your own version. If you write your own, go with atoi(), as it's much simpler.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users