LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   nasm assembler; output numbers? (https://www.linuxquestions.org/questions/programming-9/nasm-assembler%3B-output-numbers-382263/)

infinity42 11-11-2005 05:51 PM

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

Wim Sturkenboom 11-13-2005 11:42 PM

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.

bastl 11-14-2005 11:20 AM

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.

infinity42 11-15-2005 01:02 PM

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.

ntj 05-26-2018 01:22 PM

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

Thank you.


All times are GMT -5. The time now is 01:52 PM.