first assembly program

Programming, for all ages and all languages.
Post Reply
baileydr
Posts: 7
Joined: Wed Mar 28, 2007 7:32 pm

first assembly program

Post 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?
Attachments
put_integer.asm
The main assembly file
(1.72 KiB) Downloaded 39 times
linux_io.inc.fakefilename.asm
The include file (obviously)
(102 Bytes) Downloaded 66 times
baileydr
Posts: 7
Joined: Wed Mar 28, 2007 7:32 pm

Post 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]
baileydr
Posts: 7
Joined: Wed Mar 28, 2007 7:32 pm

Post 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....
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post 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" :?:
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

because the forum settings don't allow files with .inc extension
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply