LinuxQuestions.org
Visit Jeremy's Blog.
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 05-11-2004, 11:05 AM   #1
devit
Member
 
Registered: Jan 2004
Distribution: Slackware 9.1
Posts: 82

Rep: Reputation: 15
int2str problem


I tried to make a program that converts an integer to a string (for nasm) but I get an error(Floating point exception) because of the "div edx". Do you have any idea how to solve that?

Code:
section .data
numar:	resw	1
string:	resb	6
msg1:	db	"15 => "
len_msg1	equ	$ - msg1
len_string:	resb	4	
	
section .text
	global _start


int2str:			; eax = number to convert, di = the result(string)
	pusha
	mov	ebx,1
	mov	cx,0
iar:	
	mov	edx,10
	div	edx	;there is a problem
	mov	[di],edx
	mov	bx,48
	add	[di],bx
	mov	bx,0
	inc	di
	inc	cx
	cmp	eax,ebx
	jne	iar
	mov	bx,0xa
	mov	[di],bx
	popa
	mov	eax,ecx
	ret

exit:
	mov	eax,1
	int	0x80
	
_start:
	mov	edx,len_msg1
	mov	ecx,msg1
	mov	ebx,1
	mov	eax,4
	int	0x80

	lea	di,[string]
	mov	eax,15
	call	int2str
	mov	edx,eax
	mov	ecx,string
	mov	ebx,1
	mov	eax,4
	int	0x80

	call exit
Thanks!
 
Old 05-11-2004, 11:54 AM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Is there any reason you can't use the stdlib function, itoa???
 
Old 05-11-2004, 01:07 PM   #3
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Hi,

Read the nasm man page.

Usage: div <operand>

* 32 bit operand: (which edx is): EDX:EAX is divided by the given operand. So, if you set EDX to 10, then, obviously EDX:EAX goes out of bounds. Try something like this:

mov ebx, 10
div bx

Also, DI is not initialized. It's not recommended to use [di] before that. I think you want this:
mov di, bx

You're also trying to use edx later (after div, as "mov [di], edx"). What are you trying to achieve? You do know that edx would have been unchanged even if "div edx" had worked, don't you?

Arvind
 
Old 05-11-2004, 01:59 PM   #4
devit
Member
 
Registered: Jan 2004
Distribution: Slackware 9.1
Posts: 82

Original Poster
Rep: Reputation: 15
I like the idea of using itoa from stdlib but how could I implement this?
 
Old 05-12-2004, 01:18 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well... If all your code is asm, I don't know how you could implement itoa. If not however, its easy.
I am sorry, I am not too much help here, because I know nothing (or very little) of asm.
 
  


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
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

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

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

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