LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-11-2005, 05:51 PM   #1
infinity42
Member
 
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:
Code:
...
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:
Code:
...
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?

Thanks
 
Old 11-13-2005, 11:42 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

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:

Code:
// first digit
buf[0]=(pid%10)+0x30;
pid/=10;
// second digit
buf[1]=(pid%10)+0x30;
pid/=10;
 .
 .
 .
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-13-2005 at 11:49 PM.
 
Old 11-14-2005, 11:20 AM   #3
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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: beckware.de - INFO - software - NASM - examples
or direct: http://people.freenet.de/beckware/na...xtostdout.html
there are also other useful things.
Have also a look to the NASM forum:
http://sourceforge.net/forum/?group_id=6208

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

stdout:
section .data

.buf		db	'0000000000',10,0

section .text
	pushad
	
	mov	ebx,.buf+9
	mov	ecx,10
	mov	edi,10
.a:
	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

	popad
	ret
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.
 
1 members found this post helpful.
Old 11-15-2005, 01:02 PM   #4
infinity42
Member
 
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.
 
Old 05-26-2018, 01:22 PM   #5
ntj
LQ Newbie
 
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
Bumping there for telling you this solution still working perfectly.
Is clean and easy to understand.

Thank you.

Last edited by ntj; 05-26-2018 at 02:10 PM.
 
  


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

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

All times are GMT -5. The time now is 06:58 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