LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2012, 06:55 PM   #1
WarTurkey
Member
 
Registered: Jun 2009
Posts: 67

Rep: Reputation: 16
Passing args from C to NASM


Hey, I've been working on a program for a little while, I call it Inegma (I posted it on SourceForge a while ago, eh what the heck.) I'm not really good at programming, and have stepped away from it for a while. I came back and I happened to encrypt a file with my program and I forgot the code to decrypt it! I'm trying to write a program to brute force my own encryption, but I keep getting errors when I try to pass an argument to my assembly function. In my original program, I had no problems, but it's not working in this implementation:
In C, I have it written like:
Code:
char encode(char, char);
int	i,	j;
char *test_string;
...
crypto_array[i][j]=encode(j, test_string[i]);
And then the assembly file has two versions. One for 64-bit, because the arguments are passed in registers there, and another for 32-bit, because it used the stack there, but what I do is:
Code:
mov	cl,al	;because eax contains "j"
mov	al,bl	;and ebx contains "test_string[i]"
Now, I know I'm just grabbing the least significant bits of j, but because the values are only ever between 0x00 and 0x0F I thought that wasn't an issue, but maybe I'm wrong.. And I have tried both defining j as an unsigned char and casting it as one.

Last edited by WarTurkey; 01-31-2012 at 07:04 AM. Reason: Badly formatted question. I'm working on it!
 
Old 01-31-2012, 11:54 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Where do those two lines of asm code fit in?
Even in 64 bit where arguments are passed in registers, the first two arguments are not passed in rax and rbx. By x86-64 standard, the first six arguments are passed in rdi, rsi, rdx, rcx, r8 and r9 (Windows doesn't follow that standard).

Quote:
Originally Posted by WarTurkey View Post
I keep getting errors when I try to pass an argument to my assembly function.
You mean compile time errors in the C code? Or in the asm code? Or a runtime error message? Or just an incorrect result? Or what?

Last edited by johnsfine; 01-31-2012 at 11:56 AM.
 
Old 01-31-2012, 12:15 PM   #3
WarTurkey
Member
 
Registered: Jun 2009
Posts: 67

Original Poster
Rep: Reputation: 16
As I mentioned in my edit, my question is really bad. I'm going to post everything when I get home, but no, compile time, there are no errors, and it's strange that the arguments are passed that way, and that yet my original program works. I'll have it all posted when I get home.

I was actually trying to see how to delete this thread and I would post a revised form of my question later, but then I couldn't find any option to delete it.
 
Old 01-31-2012, 02:37 PM   #4
WarTurkey
Member
 
Registered: Jun 2009
Posts: 67

Original Poster
Rep: Reputation: 16
You're right, I need to move them to edi, esi (from disassembling my program), but now I don't understand how my initial program works!
 
Old 01-31-2012, 02:47 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The compiled C code will typically compute a function argument into the wrong register and then copy it to the correct register, so at the moment the asm code is called the same value is in two different registers.

But the tiniest change to the C code, the compiler version or the compile command line can change which registers are used to compute the argument values.

So I assume in your earlier version the argument values happened to be in rax and rbx as a random compilation effect as well being in rdi and rsi as a result of the x86-64 calling stantard.
 
Old 01-31-2012, 04:19 PM   #6
WarTurkey
Member
 
Registered: Jun 2009
Posts: 67

Original Poster
Rep: Reputation: 16
Ugh. Ok, this is the fourth time I've gone to type out a response. I'm getting myself completely lost. I found that I must have simply accessed the registers eax and ebx because their values were moved into edi and esi immediately before calling encode(). However, I adjusted the assembly source code to pull from esi and edi, and it seems to work for values from 0-7, but for values 8-F, it's supposed to invert the number.

It doesn't matter, the thread is essentially solved in that I was trying to grab from the wrong registers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assembly for execve passing args... trist007 Programming 3 08-12-2011 01:05 AM
[SOLVED] python passing multiple args to bash gary_in_springhill Programming 6 01-16-2011 05:14 AM
Passing args in terminal window is OK. but isn't when passing in GNOME launcher why? majrys1962 Debian 0 11-18-2008 06:00 PM
Passing args to port / make and apache rob0t *BSD 4 08-27-2008 10:34 AM
Passing args in an IOCTL neerajb Linux - Software 0 04-20-2006 05:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration