Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-11-2005, 06:51 PM   #1
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
nasm assembler; output numbers?

Probably a very simple question - I am just starting assembler. If I have something like:
mov eax,20 ;20==getpid
int 0x80
now eax should contain my processes' PID I believe? My question is this: how do I print this. I have been using:
mov eax,4 ;4==write
mov ebx,1 ;1==stdout
mov ecx,buf ;some memory address holding the string
mov edx,len ;length of string
int 0x80
which works fine for ascii strings. I can get the PID out of eax an into memory, but do I then have to convert the number into ascii, or is there a simpler way?

Old 11-14-2005, 12:42 AM   #2
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
My assembly knowledge is limited to microcontrollers and at this stage very rusty.
AFAIK you have to do the conversion yourself.

Below some pseudo-code that might help you on the way:

// first digit
// second digit
The trick is to separate each digit and convert it to an ascii value. Separation is done by using the modulo-10.
Conversion is done by simply adding 30hex.
After the conversion, we divide the pid by 10 so we can use the modulo-10 on the next digit.

Please note that the above example places the digits in reverse order in the buffer.
Hope this helps you on the way.

Last edited by Wim Sturkenboom; 11-14-2005 at 12:49 AM.
Old 11-14-2005, 12:20 PM   #3
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 235

Rep: Reputation: 21
Nice to have someone new to assembler programing

O.K. the code above you got posted is a little difficult to convert to NASM and would than be code of
(I guess) 600 lines.
And yes, you have to convert the value to ascii-numbers you can read.
In assembler you have all to do on your own in assembler there are no were lying
headers and libraries around like with c++.

Now here a procedure to convert the value of eax
from my homepage: - INFO - software - NASM - examples
or direct:
there are also other useful things.
Have also a look to the NASM forum:

;Writes the value of eax to the standard output as decimal.

section .data

.buf		db	'0000000000',10,0

section .text
	mov	ebx,.buf+9
	mov	ecx,10
	mov	edi,10
	mov	edx,0
	div	edi
	add	edx,48
	mov	[ebx],dl
	dec	ebx
	loop	.a

	mov	eax,4			;schreibe
	mov	ebx,1			;auf Terminal 
	mov	ecx,.buf
	mov	edx,11		;11 Bytes
	int	$80

What happens here:
With div edi, eax is divided by 10 and edx holds the rest. Ascii ´0´ is decimal 48 and that is added to edx and stored into .buf back-wards. Now .buf is written to STDOUT that´s all.
Old 11-15-2005, 02:02 PM   #4
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Original Poster
Rep: Reputation: 16
Ah thank you. That works great, thanks for the explaination to, I understand how it is done now! I did try to convert Wim Sturkenboom's code to NASM, but I couldn't quite do it. The DIV operator confused me somewhat, but after reading the manual carefully and looking at your code/explaination i think i understand it now.

Thanks a lot.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Nasm essoft478 Linux - Software 2 09-13-2004 05:33 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 10:18 AM
NASM, Jumping, and I'm new to this... KneeLess Programming 4 02-18-2004 03:18 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 10:16 AM
Can't ./configure nasm? therut Linux - Newbie 1 02-20-2002 11:39 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

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