Assembly language printing integers to the stdout using int 0x80
Hi all,
I'm good in programming in high level languages C/C++, python ... but a complete n00b when it comes to assembly. I want to print a single digit integer on to the screen using the int 0x80. I have loaded the number in the ecx(=5, say), size in edx (=4, lenght) and eax (=4) ebx (=1). I've added 48 to the contents of ecx, and then calling int 0x80, I thought should print 5 on the screen. However it does not print anything. also gives no errors. I checked the contents of the ecx reg (gdb), it has 53. Can someone tell me where the problem is. A snippet showing the this will be great. I've seen snippets using the c library (printf), but I want to see how it is done using int 0x80. Thanks. |
Alright, I am rather new to Assembly myself, but I think I can help you. You want to print out the number "5" on the screen, am I right? Could you provide the code you are using? I will show you how I would do it:
Code:
;print out 5 Of course, I'm using "int 80h" assuming you use linux. Look up the correct program interrupts if you use DOS or windows. |
Oh, and before I forget, you have to compile and link the program. I use "nasm" for compiling and "ld" in a shell to link it. The way you do this is quite simple.
To build the program (say I called the script above "five.asm"), you type Code:
nasm -f elf five.asm Code:
ld -s -o five five.o So remember, write the source code in a file with the suffix .asm, compile with Code:
nasm -f elf <file.asm> Code:
ld -s -o <file> <file.o> |
Hi steviebob,
Thanks for the reply, but the program snippet you wrote here is not actually printing a number. It is printing a char "5". But let us say we have to write an ASM program which adds 2 numbers and prints the sum... in that case we do not have the sum in ascii by default. So what is done usually is put the ascii equivalent of the sum in the eax register (adding 48). But when I do this it is not printing the number on stdout. I have done this using the stack reg's and the call printf statement. But I could not figure out how this is done using int 0x80. Please let me know if you can find a solution to this. Thanks. |
Quote:
Quote:
So, the code of steviebob should serve as a base for your approach ... Andi |
ForzaItalia2006, Sorry I should have rephrased the question. When I wrote :
Quote:
My question, from the very beginning, should have been how to convert from integer to ascii... pardon my poor way of framing the question.... Thanks. |
Does int 80 expect cx to contain a character to print, as yaami seems to assume, or the address of a character string, as with steviebob's code?
|
Quote:
BTW, I already told you why the value 5 can't work. I must admit that I over-read that you added 48, but that won't make any difference for the kernel, because neither adress 0x5 nor 0x33 will be addressable from the user-space. Quote:
Code:
sum_to_display = <sum of your numbers> Andi |
yaami, you seem to be ignoring the correct answers that have been posted.
Quote:
Quote:
Adding 48 is fine for converting a single digit number from an integer to ascii. You'll need more work for multi digit numbers. But it makes sense to first understand why the single digit number didn't display. Quote:
If your program computes the character(s) that you want displayed, it needs to store them somewhere in memory and put the address of where they were stored into ecx for the call to int 0x80. |
johnsfine, thanks for the reply... I'll try it out by using a variable sum and the save the sum in it and then passing it to ecx...
Thanks all. |
All times are GMT -5. The time now is 03:21 AM. |