Page 1 of 1

first assembly program

Posted: Wed Mar 28, 2007 7:38 pm
by baileydr
So this is my first program; I'm trying to get comfortable with it, so instead of copying assembly from the wiki, I can actually do it.
So, help I'm looking for:

1. Style: I feel as if I'm going about control wrong
2. Fix the segfault; if not, point me to my error
3. everything else. I need all the help I can get.
BTW, don't ask why put_char is in put_integer.asm

I could use printf, but that would defeat the purpose of the work, wouldn't it?

Posted: Thu Mar 29, 2007 1:33 pm
by baileydr
oh yea, There's a bug in it too. It puts the negative sign after the number; easily fixable.

All I'm really looking for is a pointer in the right direction...

[edit]Oh, and in response to a message, this just prints a message. It divides the number by ten, grabs the integer, and pushes it onto the stack so it prints in the right order.[/edit]

Posted: Thu Mar 29, 2007 6:49 pm
by baileydr
ok, I fixed it... I'm still a bit unsure about my conditional jumps, or if I'm doing it well or anything. So suggestions are still welcome....

Posted: Mon Apr 02, 2007 11:07 am
by ~

Code: Select all

;;linux_io.inc.fakefilename.asm

extern put_integer

%macro print_string 0
       mov eax, 4
       mov ebx, 1
       int 80h
%endmacro

Code: Select all

;;put_integer.asm

segment .data
ascii_offset	db	'0123456789ABCDEF' ; our offset for everything from b

segment .bss

Pi_sign	resb	1
	
segment .text
	global put_integer
	global print_stack
	global put_char

put_char:
	push ebp		; set up the stack
	mov ebp, esp
	push eax		; store the registers
	push ebx
	push ecx
	push edx
	mov ecx, eax		; it's nice to use eax for the argument for this 'function'
	mov eax, 4		; write
	mov ebx, 1		; stdout	
	mov edx, 1		; one byte
	pop edx			; restore the registers
	pop ecx
	pop ebx
	pop eax
	pop ebp			; and the base pointer
	ret
	
put_integer:
	push ebp
	mov esp, ebp
	push eax		; save the registers on the stack
	push ebx
	push ecx
	push edx
	mov ecx, 1		; the digit counter
	js __pi_signed
__pi_integer_loop:	
	xor edx, edx		; clear edx
	mov ebx, 10		; the 'digit offset'
	div ebx		; eax/ebx or eax/10
	add edx, ascii_offset 	; convert it to the ascii digit
	push edx		; store the remainder for printing
	cmp eax, 0		; if the quotient is zero, we've reached the end.
	jge __pi_print_stack		; so we print out the stack
	add ecx, 1		; incriment the digit loops
	jmp __pi_integer_loop	; recursive
__pi_print_stack:
	dec ecx
	pop eax			; pop the variables into the stack
	call put_char
	cmp ecx, 0
	jle __pi_print_stack		; If we've reached the last digit, we're done
	cmp byte [Pi_sign], 0x8
	je __pi_hyphen
__pi_return:	
	pop edx
	pop ecx
	pop ebx
	pop eax
	pop ebp
	ret
__pi_signed:
	;; if it's signed, then we check whether it's negative or not.
	;; if it is, then we push a hyphen in front of it.
	cmp eax, 0
	jl __pi_negative
	mov byte [Pi_sign], 0
	sal eax, 1
	jmp __pi_integer_loop
__pi_negative:
	mov  byte [Pi_sign], 0x8
	sal eax, 1
	jmp __pi_integer_loop		; jump back in!
__pi_hyphen:
	mov eax, 45
	call put_char
	jmp __pi_return
Why did you named it "linux_io.inc.fakefilename.asm"?

"Fake File Name" :?:

Posted: Mon Apr 02, 2007 12:42 pm
by Combuster
because the forum settings don't allow files with .inc extension