Recursively Called Functions and Subroutines in Assembly

Started by BrWarburto, February 03, 2007, 03:48:57 PM

Previous topic - Next topic

BrWarburto

RECURSIVELY CALLED SUBROUTINES

In the C book 'The C Programming Language' by Kernighan and Ritchie,
the authors give a very good example of a recursively called function
which gives the decimal value of a hex number (Page 87, 2nd edition).

The question arises--'Can recursively called subroutines or functions
work in assembly language?'.
It would seem that the answer is 'Yes' , provided, of course that
the stack is cleaned up on return. The example given
below illustrates the point.



;The number to be converted is
;stored in the register pair: DX:AX
;The divisor is ten, each time. and
;the remainder gives the next digit.
DATA SECTION
Text1 db 50 DUP 0h


CODE SECTION
START:
MOV EBP,ESP
MOV ESI,ADDR Text1
ADD ESI,20h
MOV AX,0h
MOV DX,0008h
MOV CX,0Ah
AGAIN:
DIV CX
PUSH EAX
PUSH ECX
ADD DL,30h
MOV B[ESI],DL
POP ECX
POP EAX
CMP AX,0
JE >  END
DEC ESI
AND DX,0000h
CALL AGAIN
END:
INVOKE msvcrt:puts,ESI
MOV ESP,EBP
RET

BeeWarb