We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Passing Arguments C -> NASM -> C

Hey all, admittedly this is some homework help.

I'm trying to write a program which takes a string in of hex characters, calls an assembler function which gives me the decimal value of the hex string. That assembler function calls a "checker" function in C which makes sure each character is a legal HEX value.

My question is two fold, how do I take an EBX register in assembler and properly pass it to a C function expecting a character. I can't seem to properly pass from assembler back to C:

#include <stdio.h>
#include <string.h>

int main(void)
{  
	char  input[255];
	int   dec_value;

	while (1)
	{
		printf ("Please enter a maximal 4 digit hex integer using a string of hex digits: ");
		scanf ("%s",input);
		if (strlen(input) <= 4)
		{
			break;
		}
		printf ("The string is too long!\n");
	}

	printf ("You entered: ");
	printf ("%s\n",input);
	extern int hex2dec(char[]);
	dec_value = hex2dec(input);
	printf ("%i",dec_value);
	if (dec_value == -1) {
		printf ("There's an invalid character!\n");
	}
	else {
		printf ("Decimal value of character %s is:%d \n", input, dec_value); 
	}		
	return 0;
}

int checkdigit (char  hex)
{
	printf (" - %c - ", hex);
	if ( (hex <= 70 && hex >= 65) || (hex >= 48 && hex <= 57) ) {
		if ( hex >= 65 ) {
			printf ("Letter");
			return ( (int) (hex-'A'+10 ));
		}
		else {
			printf ("Number");
			return hex - 48;
		}
	}
	return -1;
}
segment .data
segment .text
global  hex2dec
extern  checkdigit, printf
	
hex2dec: 	push    EBP
                mov     EBP,ESP
                push    EDX
		push	EBX

		mov     EDX,0D		   ; 0 EDX
		xor	EBX,EBX
                mov     EBX, DWORD [EBP+8]    ; copy the string to EDX
	
		push	EBX	

		call	printf		; print whole string
		add	ESP,4
		
		push	EBX
 
                call    checkdigit	   ; pass character to interpret
                add     ESP,4              ;on return clear the stack, 					         
		;the value is in EAX

		pop	EBX       
                pop     EDX		   ;restore EDX
		pop     EBP	
                ret
2
Contributors
1
Reply
1 Day
Discussion Span
1 Year Ago
Last Updated
3
Views
nasmnoob
Newbie Poster
1 post since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I think you correctly pass the value of EBX to the checkdigit function by placing it on the stack before the call.

However, I am not sure of the standard calling convention used by the C compiler, but chances are that it is not cdecl (as for printf) and in that case the caller does not need to adjust the stack pointer once the call returned. In simple terms, Line 23 in your assembly code seems unnecessary.

pgcoder
Newbie Poster
6 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2834 seconds using 2.7MB