Assembler Developer

Moderators: None (Apply to moderate this forum)
Number of threads: 1030
Number of posts: 1832

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
ASCII print to stdout (console) in NASM/Linux Posted by holland01 on 29 Jun 2012 at 3:04 PM
Hi, I'm trying to write an assembly programming which multiplies the values stored in two registers, and converts the result to ASCII. I need to know what exactly what it is that I'm doing wrong, as I keep getting segmentation faults and/or floating point exceptions. The comments in the code should explain in detail how I'm trying to accomplish this, but I find that most of my issues keep happening when I try do div edx by eax :/

Any thoughts? I think I have to actually *initialize* the registers, though I don't quite know how to do that. Thanks.

Code

section .data
	counter: db 0xA							;store value 10 in 'counter', while allocating only one byte. This will be used for decrementing purposes
section .bss
	valueToPrint: resb 4					;alloc 4 bytes of data in 'valueToPrint'

section .text

global _start

_print_char: 					
	add eax, '0' 				;convert to ascii
	mov [valueToPrint], eax		;store contents of 'eax' in valueToPrint
	mov eax, 4					;syswrite
	mov ebx, 1					;stdout
	mov ecx, valueToPrint		;machine will take whatever value exists in 'ecx' and print
	mov edx, 1					;print only a single byte's worth of data
	int 0x80					;invoke kernel to perfrom instruction
	ret							

_convert_values:
	mov edx, 0xA				;dividing eax by 10, which will lower its tens place
	div edx						;do division: remainder SHOULD be stored in edx
	mov byte [edx], 0x0			;zero out edx		
	call _print_char			;do printing for latest character
	dec byte [counter]			;decrement counter
	mov dword [eax], counter	;store counter in eax
	jnz _convert_values			;while eax > 0 continue process

_endl:
	mov eax, '\n'				;store newline character in eax to be printed
	call _print_char			;print value
	ret					

_mul:
	mov eax, 0x2A ;store 42 in eax
	mov edx, 0x2B ;store 43 in edx
	mul edx		  ;multiply [eax] * [edx]
	ret

_safe_exit:
	mov eax, 1 	;initiate 'exit' syscall
	mov ebx, 0 	;exit with error code 0
	int 0x80	;invoke kernel to do its bidding 

_start:
	nop								;used to keep gdb from complaining

	call _mul						;multiply the values
	call _convert_values			;do hex to ascii conversion
	
	jmp _safe_exit					;use jmp as opposed to call since it technically doesn't 'ret'


Thread Tree
holland01 ASCII print to stdout (console) in NASM/Linux on 29 Jun 2012 at 3:04 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.