You are not logged in.

#1 2012-09-03 13:27:36

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

[Solved] Assembly - Passing Integer Command Line Parameters

This is as good a place to ask as any I suppose, so here goes.

I'm really only interested in knowing if it is at all possible to push actual values, in this case integers, on the stack from the command Line, i.e. passing arguments and if so how, because as far as I can gather, it is simply impossible to pass anything other than strings.

Yes I am a noob at assembly, if anyone should doubt that. wink


Best regards.

I thought the best thing to do is simply to explain what I'm actually doing.

In short, I need to derive the Lightness/Luminance level of a pixel. This of course can be done a number of way. Initially I experimented with image magic, but when I couldn't figure it out, I thought: "Hey, why not learn something along the way?"

So here I an, trying to do some assembly under linux.

For those interested the process is actually rather sinbple:

1. get and convert the rgb input as needed.

2. Determine the highest and lowest channel (progress so far):

mov ax, $1;
mov cx, $2;
mov dx, $3;
cmp ax, cx;
jng else;
mov ax, ax;
mox bx, cx;
jmp next;
else:
mov ax, cx;
mov bx, ax;
next:
cmp dx, ax;
jng else2;
mov ax, dx;
jmp end;
else2:
cmp bx, dx;
jnl end;
mov bx, dx
end:

3. add the two values and divide by two. possible implement a way to check if the addition is even or not, so to enable proper rounding.

4. output the result in one form or another.

Last edited by zacariaz (2012-09-03 20:30:14)


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#2 2012-09-03 14:24:42

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Assembly - Passing Integer Command Line Parameters

I don't know if this is what you want, but with bash you pass hex values to a command like this:

./command $'\xff\xff'

Offline

#3 2012-09-03 14:58:35

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Procyon wrote:

I don't know if this is what you want, but with bash you pass hex values to a command like this:

./command $'\xff\xff'

This will still appear as a string with the first two characters being 0xff.

zacariaz, this really isn't a feature of the assembly language, but rather how the shell calls programs (someone more knowledgable might correct or expand on this).  The best solution I can give you is to take a look at libc's atoi().  You should be able to call that from assembly.

Offline

#4 2012-09-03 15:28:22

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

cmtptr wrote:
Procyon wrote:

I don't know if this is what you want, but with bash you pass hex values to a command like this:

./command $'\xff\xff'

This will still appear as a string with the first two characters being 0xff.

zacariaz, this really isn't a feature of the assembly language, but rather how the shell calls programs (someone more knowledgable might correct or expand on this).  The best solution I can give you is to take a look at libc's atoi().  You should be able to call that from assembly.

Maybe I formulated my question badly, but you are of course right in that assembly has little to do with it, though it is the reason I need to know.

Anyhow, I believe it is the case that when you us command line parameter, they are pushed on the stack, that is first the number of parameters, then the name of the application and then the parameters, or something similar at least.

What I asked was really a yes or no question: Is it possible to push an integer on to the stack?

I rather assume the answer is no, which annoys the hell out of me, but better to be sure.


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#5 2012-09-03 15:48:08

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: [Solved] Assembly - Passing Integer Command Line Parameters

zacariaz wrote:
cmtptr wrote:
Procyon wrote:

I don't know if this is what you want, but with bash you pass hex values to a command like this:

./command $'\xff\xff'

This will still appear as a string with the first two characters being 0xff.

zacariaz, this really isn't a feature of the assembly language, but rather how the shell calls programs (someone more knowledgable might correct or expand on this).  The best solution I can give you is to take a look at libc's atoi().  You should be able to call that from assembly.

Maybe I formulated my question badly, but you are of course right in that assembly has little to do with it, though it is the reason I need to know.

Anyhow, I believe it is the case that when you us command line parameter, they are pushed on the stack, that is first the number of parameters, then the name of the application and then the parameters, or something similar at least.

What I asked was really a yes or no question: Is it possible to push an integer on to the stack?

I rather assume the answer is no, which annoys the hell out of me, but better to be sure.

You are correct, as far as I know the answer is no.  At the shell level you have no control over what happens on the stack.  That is sort of by definition the nature of a high level langauge.

Offline

#6 2012-09-03 16:07:20

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Well, at the same time it makes sense and it don't.

I makes sense to have a "common language", so to speak, so that various applications can be piped together.
At the same time however, it's a waste to have the majority of the code dedicated to making conversions. First one for the input and then one for the output.

But that life I guess.


Thanks for now.


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#7 2012-09-03 16:12:52

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [Solved] Assembly - Passing Integer Command Line Parameters

This is all very interesting, but why go to the trouble of assembly language?

This seems the perfect solution for C.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#8 2012-09-03 16:14:38

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

ewaller wrote:

This is all very interesting, but why go to the trouble of assembly language?

This seems the perfect solution for C.

The best way of learning is by doing, but in order to learn by doing, obviously you need something to do and this seemed like the perfect opportunity.

edit:
apart from that you are of course right.

Last edited by zacariaz (2012-09-03 16:16:58)


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#9 2012-09-03 16:16:40

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Fair enough smile


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#10 2012-09-03 16:25:39

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: [Solved] Assembly - Passing Integer Command Line Parameters

zacariaz wrote:

Well, at the same time it makes sense and it don't.

I makes sense to have a "common language", so to speak, so that various applications can be piped together.
At the same time however, it's a waste to have the majority of the code dedicated to making conversions. First one for the input and then one for the output.

But that life I guess.


Thanks for now.

What you're describing is basically that this conversion happens on the shell's side rather than on the called program's side.  It happens either way.

In the grand scheme of things, the interface between programs accounts for only a very small portion of the total processor's time.  This is something that usually happens only once per application, and generally at the very beginning or the very end.  So keeping it standardized and human readable is well worth the overhead in my opinion.

I suppose if it really bothered you, you could get away with embedding your values into a single input parameter using Procyon's method.  They wouldn't be directly on the stack, but you'd have a pointer to them.  It might be worth it to play around with the idea, but I totally would not recommend it for anything you intend to maintain or expect anyone else to maintain tongue.

Offline

#11 2012-09-03 20:24:47

Lux Perpetua
Member
From: The Local Group
Registered: 2009-02-22
Posts: 69

Re: [Solved] Assembly - Passing Integer Command Line Parameters

zacariaz wrote:

Well, at the same time it makes sense and it don't.

I makes sense to have a "common language", so to speak, so that various applications can be piped together.
At the same time however, it's a waste to have the majority of the code dedicated to making conversions. First one for the input and then one for the output.

But that life I guess.


Thanks for now.

When you run a program on a Linux system, it ultimately all boils down to the following function:

int execve(const char *path, char *const argv[], char *const envp[]);

Without going into full detail: the first argument is the path to the program being executed. The second argument is literally the "argv" argument list the program sees (which is a null-pointer-terminated array of pointers to null-character-terminated strings). The third argument is the environment. Relevant to you is the second argument. It doesn't matter if it's an assembly program, a C program, or a shell script; the "command-line" arguments to the program, i.e., the argv array, are pointers to strings.

Lest you have the brilliant idea to take an integer apart into bytes and stick them in a string as one of the argument as some others have suggested, you should consider the following: what if one of the bytes of the integer is 0? That technically ends the "string," and everything following that byte is discarded by execve. Adding 1 to all the bytes wouldn't work, since 0xff would wrap around to 0 again. To make it work, you'd need to encode the integer in some other complicated way to avoid null characters and then decode it inside your program. In other words, you might as well just pass an ordinary string and parse it with atoi or strtol.

Offline

#12 2012-09-03 20:29:54

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Lux Perpetua wrote:
zacariaz wrote:

Well, at the same time it makes sense and it don't.

I makes sense to have a "common language", so to speak, so that various applications can be piped together.
At the same time however, it's a waste to have the majority of the code dedicated to making conversions. First one for the input and then one for the output.

But that life I guess.


Thanks for now.

When you run a program on a Linux system, it ultimately all boils down to the following function:

int execve(const char *path, char *const argv[], char *const envp[]);

Without going into full detail: the first argument is the path to the program being executed. The second argument is literally the "argv" argument list the program sees (which is a null-pointer-terminated array of pointers to null-character-terminated strings). The third argument is the environment. Relevant to you is the second argument. It doesn't matter if it's an assembly program, a C program, or a shell script; the "command-line" arguments to the program, i.e., the argv array, are pointers to strings.

Lest you have the brilliant idea to take an integer apart into bytes and stick them in a string as one of the argument as some others have suggested, you should consider the following: what if one of the bytes of the integer is 0? That technically ends the "string," and everything following that byte is discarded by execve. Adding 1 to all the bytes wouldn't work, since 0xff would wrap around to 0 again. To make it work, you'd need to encode the integer in some other complicated way to avoid null characters and then decode it inside your program. In other words, you might as well just pass an ordinary string and parse it with atoi or strtol.

You are of course right, and thanks for the very nice explanation, however it's still a pain in the but. wink
I like working with number, not with letters, but enough of this.

Thanks to all and to all a good night, day or whatever you please.


Best regards


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#13 2012-09-03 21:43:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Assembly - Passing Integer Command Line Parameters

I've read this a couple times and I still don't get it.

The computer doesn't pass numbers or letters.  It passes bytes.  It is entirely up to you how you interpret those bytes of data - especially in assembly.

C has different data types for characters and numbers, but really that just is a convenience.  For example, the following C code would add a number (1) to a letter and output the letter g:

char character='f';
character++;
puts(character);

This is because the bit values that represent 'g' are one higher than 'f'.

When you pass numbers as parameters in a shell, the problem isn't the type of data being passed (it's still just bytes), the problem is when you type

$ programname 1 4 8

programname is not passed the values 00000001, 00000100, and 00001000, which would represent those numbers, instead it is passed the values 00110001, 00110100, and 00111000 which represent the characters 1 4 and 8 that were given to the shell.

Crap in crap out.


Your code willmight work perfectly, as long as it is passed a binary representation of the numerical value you intended to pass it.

*might = I have checked.

Last edited by Trilby (2012-09-03 21:49:11)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#14 2012-09-03 21:49:33

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Trilby wrote:

...

I think Lux explained it nicely. \0 end the string, so even if I were to pass a binary representation, I'd have to be damn sure it didn't contain any zeros.

In any case, I got the answer I was looking for. I may not like it, but I don't like a lot of things, like mushrooms, babies and statistics (IT'S NOT MATH I TELL YOU!!!).


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#15 2012-09-03 21:50:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Er ... you can pass zero's just fine.

A zero byte terminates a string - but you should not be interpreting it as a string anyhow!

But all that aside, using a file descriptor or a memory address would be much easier anyway.  One process puts data there, the other reads it.

Last edited by Trilby (2012-09-03 21:56:39)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#16 2012-09-03 22:03:41

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Trilby wrote:

Er ... you can pass zero's just fine.

A zero byte terminates a string - but you should not be interpreting it as a string anyhow!

But that's how the stack works ain't it?

Anyway, as I said, it doesn't matter.


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#17 2012-09-04 01:57:58

Lux Perpetua
Member
From: The Local Group
Registered: 2009-02-22
Posts: 69

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Trilby wrote:

Er ... you can pass zero's just fine.

A zero byte terminates a string - but you should not be interpreting it as a string anyhow!

But all that aside, using a file descriptor or a memory address would be much easier anyway.  One process puts data there, the other reads it.

As far as I can see, there's no good way to execute a program in such a way that some argv[k] has an embedded null byte. For all of the exec* family of functions, null characters have the special interpretation of ending the string they're in. Nothing prevents you from passing {"progname", "\x00\x01\x02", (char *)0} as argv, but say goodbye to '\x01' and '\x02' before calling execve, execv, etc.

Offline

#18 2012-09-04 02:08:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Assembly - Passing Integer Command Line Parameters

It wouldn't be "embedded" anywhere.  But you know that it's there because that "string" is terminated.

My point is if it's binary data, don't interpret it as strings.

If your program is expecting 32 bytes of data then either the sum total of the srtlen's of all the arguments should be 32, or incorrect parameters were passed.  Whether thats 32 bytes in argv[1] or 10 bytes in argv[1] and 21 bytes in argv[2] plus the zero in between is irrelevant if you stop trying to interpret a stream of data as a string.

Last edited by Trilby (2012-09-04 02:09:06)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#19 2012-09-04 05:50:55

Lux Perpetua
Member
From: The Local Group
Registered: 2009-02-22
Posts: 69

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Trilby wrote:

It wouldn't be "embedded" anywhere.  But you know that it's there because that "string" is terminated.

My point is if it's binary data, don't interpret it as strings.

If your program is expecting 32 bytes of data then either the sum total of the srtlen's of all the arguments should be 32, or incorrect parameters were passed.  Whether thats 32 bytes in argv[1] or 10 bytes in argv[1] and 21 bytes in argv[2] plus the zero in between is irrelevant if you stop trying to interpret a stream of data as a string.

I don't think you can rely on the constituents of argv being contiguous, though (whether or not they happen to be stored contiguously in the calling process). I've never read any documentation that suggests they would be, and this is not behavior I'd ever rely on without documentation. The called process would then need to concatenate one or more of its arguments to get that single logical byte-sequence argument . This falls under "encod[ing] the integer in some other complicated way to avoid null characters and then decod[ing] it inside your program" as I said in my other post.

Offline

#20 2012-09-04 07:28:28

zacariaz
Member
From: Denmark
Registered: 2012-01-18
Posts: 539

Re: [Solved] Assembly - Passing Integer Command Line Parameters

Trilby wrote:

It wouldn't be "embedded" anywhere.  But you know that it's there because that "string" is terminated.

My point is if it's binary data, don't interpret it as strings.

If your program is expecting 32 bytes of data then either the sum total of the srtlen's of all the arguments should be 32, or incorrect parameters were passed.  Whether thats 32 bytes in argv[1] or 10 bytes in argv[1] and 21 bytes in argv[2] plus the zero in between is irrelevant if you stop trying to interpret a stream of data as a string.

I understand what you're saying now, however I don't think it would be a viable solution in the long run.

Of course, in my case the argument need only be 32 bit and if zero, it doesn't really make sense to input it. Stiller, better do things as people would expect I think.


I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.

Offline

#21 2012-09-06 10:00:11

drobole
Member
From: Norway
Registered: 2012-07-23
Posts: 125

Re: [Solved] Assembly - Passing Integer Command Line Parameters

deleted

Last edited by drobole (2012-09-06 10:23:10)

Offline

Board footer

Powered by FluxBB