flat assembler
Message board for the users of flat assembler.

Index > Linux > How do I print integers, not their ASCII equvalent?

Author
Thread Post new topic Reply to topic
linux0id



Joined: 22 Jul 2007
Posts: 2
Location: Moscow
linux0id 22 Jul 2007, 16:38
Hello everyone,
Not so long ago I decided to learn assembly language. I have grasped the basics of it more or less, but there is one problem that I cannot solve (and find any help about). I wrote a program to output the sum of two integers that are entered by the user. I'm using the kernel sys_call write. It seems to add them up correctly, but it prints out the ASCII symbol of that number (or similar)! I've tried YASM, FASM and NASM, but the result is the same. Can anyone please give me some advice as to how to output the actual number, not the ASCII code? Here's the source code -
Code:
....

section .bss
     input1 resd 1
     input2 resd 1
     sum    resd 1
section .text

    global _start
    _start:

....

     mov      rax,[input1]
     add      rax,[input2]
     mov      [sum], rax

     ; print sum
     mov      rdx,4
     lea      rcx,[sum]
     mov      rbx,1
     mov      rax,4
     int 0x80    
Post 22 Jul 2007, 16:38
View user's profile Send private message ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 22 Jul 2007, 16:48
You need to convert the integer to a string manually - use the age-old "divide and conquer" method, keep dividing by 10 and using the modulus until the source number is zero.
Post 22 Jul 2007, 16:48
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 25 Jul 2007, 02:17
Post 25 Jul 2007, 02:17
View user's profile Send private message Visit poster's website Reply with quote
linux0id



Joined: 22 Jul 2007
Posts: 2
Location: Moscow
linux0id 26 Jul 2007, 09:00
Thanks all of you for your help! I am a noob in programming (just basic knowledge of FORTRAN), so it will take a a while to think through the code in those threads that rugxulo gave me.
Post 26 Jul 2007, 09:00
View user's profile Send private message ICQ Number Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 25 Oct 2007, 17:28
f0dder wrote:
You need to convert the integer to a string manually - use the age-old "divide and conquer" method, keep dividing by 10 and using the modulus until the source number is zero.
just 1 little thing:
What you mean is not divide-and-conquer in a "usual" sense, it's a plain iterative algorithm, usually called something like "Horner number base" algorithm
Post 25 Oct 2007, 17:28
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 25 Oct 2007, 23:17
True, divide-and-conquer is more appropriately used to describe things like binary search. You do use division to conquer this task, though Wink
Post 25 Oct 2007, 23:17
View user's profile Send private message Visit poster's website Reply with quote
rob.rice



Joined: 20 Dec 2003
Posts: 54
rob.rice 05 Dec 2007, 09:40
there is an example subroutine to do just this in the fasm source code
it's in the system.inc file the subroutine is labeled display_number:

use the force read the source
Post 05 Dec 2007, 09:40
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.