Jump to content

Recursion in TASM

- - - - -

  • Please log in to reply
1 reply to this topic

#1
assembler_wanna_be

assembler_wanna_be

    Newbie

  • Members
  • Pip
  • 3 posts
I've tried to write some recursive functions in assembly but when I couldn't for two days I decided to ask for some help. Here are my two attempts which are both trying to output a simple digit triangle. Thank you in advance!


.model small


.data

	num_iter dw 6

	endl db 0Ah, 0Dh, '$'

.stack 100h


.code


main proc

	mov ax, @data

	mov ds, ax

	mov bx, num_iter

	push bx

	call recursive

	pop bx

	

	mov ax, 4Ch

	int 21h

main endp


recursive proc

	push bp

	mov bp, sp

	

	mov bx, [bp]+4

	or bx, bx

	jz end_rec

	

        mov ah, 2

	mov dx, 30h

	mov cx, bx

	loop1:

		int 21h

		inc dx

	loop loop1


        mov ah, 9

        lea dx, endl

        int 21h


	dec bx

	push bx

	call recursive

	pop bx

	end_rec:

	pop bp

	ret

recursive endp


end main



And the other one without explicit manipulation of the stack


.model small


.data

	iter dw 6

	endl db 0Ah, 0Dh, '$'

	

.stack 100h


.code

main proc

	mov ax, @data

	mov ds, ax

	

	call recur pascal, iter

	

	mov ax, 4Ch

	int 21h

main endp


recur proc pascal near

arg n:byte

uses dx, cx


	cmp n, 0

	je out_of

	mov cl, n

	mov dl, 30h

        mov ah, 2

	loop1:

		int 21h

		inc dl

	loop loop1

	

        mov ah, 9

	lea dx, endl

	int 21h

	

	mov dx, iter

	dec dx

	mov iter, dx

	

	call recur pascal, iter

	

	out_of:

	ret

recur endp


end main


Since asking about it in a forum is pretty much my last resort any help will be appriciated. :)

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,251 posts
  • Location:C:\Countries\US
I don't think this one part matters too much, but I still decided to mention it:

Quote

.data

	num_iter dw 6

	endl db 0Ah, 0Dh, '$'
It's just that a new-line character is:
endl db 0Dh, 0Ah, ...
(so it's 13, 10, and not 10, 13), but, once again, I think this part is not too important.

There's this other part that I noticed:

Quote

recursive proc

	push bp

	mov bp, sp

	

	mov bx, [bp]+4

	or bx, bx

	jz end_rec

	

        mov ah, 2

	mov dx, 30h

	mov cx, bx

	loop1:

		int 21h

		inc dx

	loop loop1


        mov ah, 9

        lea dx, endl

        int 21h


	dec bx

	push bx

	call recursive

	pop bx

	end_rec:

	pop bp

	ret

recursive endp
It's this part:
	end_rec:

	pop bp

	ret

recursive endp
that I think should be something like this:
end_rec: 

mov sp, bp 

pop bp 

ret 

recursive endp 
There's also this, that I noticed:

Quote

recur proc pascal near

arg n:byte
I don't know how TASM works, but it's just that 'iter' is a word value, while the argument for the proc is a byte value; but, once again, I'm not sure how TASM handles this.

These are just some things I noticed, when looking at your code, but I am not sure about what the problem might be in.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users