Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Integers in NASM

Status
Not open for further replies.

klose86

Programmer
Aug 1, 2006
13
CR
HI everyone

I'm a newbbie in assembly, but trying to learn. Does anyone know how do I print integer values on the screen? I'm working on Linux with the NASM compiler.
I know how to print strings, like the classic "hello world!", but it doesn't work for numbers.

I defined the variables this way:

L1 db 100
L2 db 450

I want to add both numbers and the print the result.

Thank you very much
 
You have to convert the numeric values to their representative ASCII string before printing. Developing this algorithm involves a learning process every programmer must go thru on the road to developing more complex algorithms, which is why this assignment is one of the first ones handed out by instructors. If we were to give it to you on a silver platter, you will have not learned one of the foundations of assembly prgramming. But I will give you a hint: how are ASCII character Codes stored in memory?

The simplest solution is the best!
 
I'm not familiar with NASM syntax, so I can't help you there. However, here's a short program that demonstrates how to print integers in assembly using the C function library, using Gnu Assembler syntax:

Code:
.section	.data
format:	.asciz	"L1 = %d  L2 = %d\n"
L1:	.int	100
L2:	.int	450

.section	.data
	.global	_start
	.align	4

_start:
	pushl	L2
	pushl	L1
	pushl	$format
	call	printf

	movl	$1, %eax
	int	$0x80
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top