flat assembler
Message board for the users of flat assembler.

Index > Linux > problem printing output to screen

Author
Thread Post new topic Reply to topic
tgo



Joined: 18 Nov 2005
Posts: 3
tgo 18 Nov 2005, 20:29
I have written a function to calculate the factorial of a number in nasm and I verfied the number is calculating correctly by externing printf and printing it but I want to not have to extern anyhting.

The variable used was declared in .bss and I was wondering if it would be possible to print it using int 80h. I tried using write() and printing it but nothing prints. I know that write takes a string and prints so I am aware I will probally have to convert the numbers to a string, but then after that I am still stuck on what address to put?

So basiclly how can you use write() to print someting from .bss and if you cant what do you use?

Im using nasm on slackware.
Post 18 Nov 2005, 20:29
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 19 Nov 2005, 00:58
You'd need to convert the number to a string, then pass the write() system call the address of the string and its length and the device to write the string to. As a simple example:

Code:
format elf executable
mov ecx, string_address                 ; address of string
mov edx, [string_address.length]        ; length of string
mov ebx, 1                              ; stdout
mov eax, 4                              ; sys_write
int $80

mov eax, 1                              ; sys_exit
int $80                                 ;

string_address db 'hello, world!'
.length dd $ - string_address    
Post 19 Nov 2005, 00:58
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 19 Nov 2005, 17:14
The conversion routine is a bit tricky but for a real programmer it's never a problem Smile.
Code:
format ELF executable
entry start

section readable executable

start:
; calculate factorial of eax
        mov     eax, 5
        call    factorial
; convert result to string
        mov     ecx, buffer
        mov     edx, buffer.size
        call    convert_uint32_to_str
; print the string
        mov     eax, 4
        mov     ebx, 1
        int     0x80
; exit
        mov     eax, ebx
        xor     ebx, ebx
        int     0x80

; input:
; eax - unsigned value whose factorial is to be calculated
; output:
; eax - factorial
factorial:
        cmp     eax, 1
        jbe     .finish
        lea     ecx, [eax-1]
.loop:
        mul     ecx
        loop    .loop
.finish:
        ret

; inputs:
; eax - unsigned value to be converted
; ecx - buffer the string is to be written to
; edx - length of buffer
; outputs:
; ecx - offset of string
; edx - length of string
convert_uint32_to_str:
        xor     edi, edi
        add     ecx, edx
        mov     ebx, 10
.loop:
        dec     ecx
        inc     edi
        xor     edx, edx
        div     ebx
        add     dl, '0'
        mov     [ecx], dl
        test    eax, eax
        jnz     .loop
.finish:
        mov     edx, edi
        ret

section writeable

buffer rb 16
.size = $ - buffer
    
Post 19 Nov 2005, 17:14
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.