Practice problems, do not submit for the lab report



Download 0.89 Mb.
Page1/3
Date23.11.2022
Size0.89 Mb.
#60016
  1   2   3
Lab 11 SMS factorial RECURSIVE, ITERATIVE, LRU-cache-replacement

3443 Computer Architecture Lab 11



Practice problems, do not submit for the lab report. The section for the lab report is at the end.
Procedures in SMS/x86 – continued. The FACTORIAL
1] Solve Task 25 from the SMS documentation:
Write a recursive procedure that works out Factorial N. Use the stack for parameter passing. Use this definition of Factorial:

  • Factorial ( 0 ) is defined as 1.

  • Factorial ( N ) is defined as N * Factorial (N - 1).

To calculate Factorial (N), the procedure first tests to see if N is zero and if not then re-uses itself to work out N * Factorial (N - 1).
Use this starter code:
; ----- Main program for recursive factorial ----------------
MOV AL,3 ; Do not try n>5, it overflows
PUSH AL ; Parameter is passed on the stack
CALL 60
POP AL ; Factorial is here
OUT 01 ; Output the result to port 01 (traffic lights)
HALT
; ----- Procedure for recursive factorial--------------------
ORG 60 ; Code starts at address [60]
POP DL ; Return address
POP AL ; Only parameter


MOV CL, 0
CMP AL, CL
JNZ Recu


Base: MOV AL, 1 ; If n==0, n! is 1
PUSH AL
PUSH DL
RET


Recu: PUSH AL ; Rebuild stack frame for current call
PUSH DL


PUSH AL ; Copy AL in BL before decrementing (1/2)
POP BL ; Copy AL in BL before decrementing (1/2)


DEC AL ; n = n-1
PUSH AL ; Pass on stack for recursive call
CALL 60 ; recursive call
POP AL ; here we have (n-1)!


MUL AL, BL ; n*(n-1)!

Download 0.89 Mb.

Share with your friends:
  1   2   3




The database is protected by copyright ©ininet.org 2024
send message

    Main page