Linux Assembly - how to output numbers instead of ASCII symbols

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #1
Jul 22nd, 2007
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 give me some advice as to how to output the actual number, not the ASCII code? Here's the source code -

  1. ....
  2. section .bss
  3. input1 resd 1
  4. input2 resd 1
  5. sum resd 1
  6. section .text
  7. global _start
  8. _start:
  9. ....
  10. mov rax,[input1]
  11. add rax,[input2]
  12. mov [sum], rax
  13.  
  14. ; print sum
  15. mov rdx,2
  16. lea rcx,[sum]
  17. mov rbx, 1
  18. mov rax,4
  19. int 0x80

Please help or give some advice as to how I can get rid of this! Without solving this problem I cannot continue any further.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #2
Jul 22nd, 2007
what you are doing is attempting to print the binary value of the number. It has to be converted to ASCII before it can be printed. If you look at an ascii chart you will see that the ascii value for '0' is 48 decimal, or 30H. So if you want to print the 0 digit you have to add 48 to make it printable on the screen.

using a loop your program needs to split the value of sum up into its individual digits and add 30H to the digit before printing it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #3
Jul 22nd, 2007
Thanks a lot for your reply. I will try it out soon, at least for singel digits. Is there a simpler way of fixing it?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #4
Jul 22nd, 2007
If your application is pure assembly then the way AncientDragon explained it is the only way unless you use BCD or want to display result in decimal. A lot of applications I do are for windows therefore I use wsprintf a function of kernel32.lib and then one of the parameters such as %d or decimal or %X for hex will do the conversion for me
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #5
Jul 22nd, 2007
Guess I'll just have to try it then. Thanks a lot anyway!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #6
Jul 22nd, 2007
If you really get stuck I can post a short snippet on the example Ancient Dragon gave you. Just to clarify you are using NASM on an intel based machine that uses Linux. The reason I ask is that I'm assuming in your first post rax actually means eax.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #7
Jul 22nd, 2007
I'll be away for a bit so I'll give you the snippet regardless. This is written for an XP based machine, but I'm sure you'll be able to improvise.

  1. push edi
  2. mov al,30H ; Ascii equivalent to "0"
  3. mov edx, 913387 ; You can make this any value
  4. std ; set EDI to auto decrement
  5. mov edi, 401087
  6. .D0 mov al, dl
  7. and al, 15 ; strip bits 7 - 4
  8. or al, 30H
  9. stosb
  10. shr edx, 4 ; shift next digit
  11. jnz .D0
  12. mov eax, edi
  13. inc eax
  14. pop edi
  15. cld
  16. pop edi
This method leaves base address of string in EAX, but you can put it anywhere.

This only works if you are displaying decimal digits. You'll have to modify loop if you want hex in order to display A - F.
Last edited by Tight_Coder_Ex; Jul 22nd, 2007 at 4:40 pm. Reason: Clarification
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 37
Reputation: linux0id is an unknown quantity at this point 
Solved Threads: 0
linux0id's Avatar
linux0id linux0id is offline Offline
Light Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #8
Jul 23rd, 2007
Thanks a lot! I managed to do it for one digit, but couldnt think how to do it for two or more! The machine that I'm using is AMD64 bit, so the register rax is equvalent to eax, but just is 64 bit. The assembler is YASM, which fully supports NASM's syntax. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: someb0dy is an unknown quantity at this point 
Solved Threads: 1
someb0dy someb0dy is offline Offline
Newbie Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #9
Aug 10th, 2007
Originally Posted by linux0id View Post
I managed to do it for one digit, but couldnt think how to do it for two or more!
I got the same problem. I managed to do it for one digit, but it's just impossible to do it for more.
I'm getting mad about this ><
^^

I'm using MASM32,but with a 16bit Linker.

Can anybody please give me a hint, what i made wrong ?

This is my source code.

  1.  
  2.  
  3.  
  4.  
  5. DUDU SEGMENT
  6.  
  7. Affe db "Hallo Das einzige was das Programm hier tut, ist so eine Zeile auszugeben(derzeit nicht ;-))"
  8. db "$"
  9.  
  10. Fenster db ?
  11.  
  12. db "$"
  13.  
  14. DUDU ENDS
  15.  
  16. TAM SEGMENT
  17.  
  18. ASSUME CS:TAM,DS:DUDU
  19.  
  20. Anfang: mov ax,DUDU
  21. mov ds,ax
  22.  
  23.  
  24. mov Fenster,85
  25.  
  26. lea di,Fenster
  27.  
  28.  
  29. add Fenster,48
  30.  
  31. inc di
  32.  
  33. add Fenster,48
  34.  
  35.  
  36. lea dx,Fenster
  37.  
  38.  
  39.  
  40. mov ah,09
  41.  
  42. int 21h
  43.  
  44.  
  45.  
  46. mov ah,4Ch
  47.  
  48. int 21h
  49.  
  50. TAM ENDS
  51.  
  52. END Anfang
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Linux Assembly - how to output numbers instead of ASCII symbols

 
0
  #10
Aug 10th, 2007
#1: When you move 85 55H into Fenster your result will be two ascii digits. What you've done is added 48 to 85 and then 48 to your terminator $. This might help you out
  1. Fenster db 85, 0, '$'
  2.  
  3. mov dx, offset Fenster
  4. mov ax, Fenster
  5. push ax
  6. and ax, 15
  7. add ax, 48
  8. mov dx, al
  9. inc dx
  10. pop ax
  11. shr ax, 4
  12. add ax, 48
  13. mov dx, al
  14. dec dx
I'm not advicating this is the best way to do it, but based on your logic this is probably the closest approximation based on your code
Last edited by Tight_Coder_Ex; Aug 10th, 2007 at 4:57 pm. Reason: computer has a mind of its own
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 5134 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC