ECE 2560 L 12 Computing Factorial Department of

  • Slides: 13
Download presentation
ECE 2560 L 12 -Computing Factorial Department of Electrical and Computer Engineering The Ohio

ECE 2560 L 12 -Computing Factorial Department of Electrical and Computer Engineering The Ohio State University ECE 3561 - Lecture 1 1

HLL to Assembler l Pseudo HLL l HLL structure Their flow chart l HHL

HLL to Assembler l Pseudo HLL l HLL structure Their flow chart l HHL code l Corresponding Assembler l ECE 3561 - Lecture 1 2

Computing n! l What is n! ? l l l n factorial is n*(n-1)*(n-2)*…(1)

Computing n! l What is n! ? l l l n factorial is n*(n-1)*(n-2)*…(1) n! = n*(n-1)! This can be done recursively l l Have a subroutine to compute n! In the computation is n=1 then result is 1 If n>1 then the value is n*(n-1)! And to compute (n-1)! the routine called again ECE 3561 - Lecture 1 3

Pseudocode l Factorial – n! function nfact (var n) return z; l if (n=1)

Pseudocode l Factorial – n! function nfact (var n) return z; l if (n=1) then z = 1; l else z = n * nfact(n-1); l end if; l end nfact; l ECE 3561 - Lecture 1 4

Features of recursive routines Can have no local data that could be modified in

Features of recursive routines Can have no local data that could be modified in the recursive call l Data needs to be on the stack l Any register used must be restored before the routine returns. l Because there is no local data and no absolute addresses the routine is also “position independent” l Position independent – code that can be run in any location in memory. l ECE 3561 - Lecture 1 5

Factorial - recursion l Arguments l The number n to find n! of is

Factorial - recursion l Arguments l The number n to find n! of is pushed on TOS l Result l will be passed on the stack. is returned on the stack. The value of n! is returned on the TOS ECE 3561 - Lecture 1 6

The routine l. N is passed on the stack so the stack looks like

The routine l. N is passed on the stack so the stack looks like this when entering routine l So start by saving the state of the processor ECE 3561 - Lecture 1 7

The code l l l l l fact push SR push R 5 push

The code l l l l l fact push SR push R 5 push R 6 push R 7 mov 10(SP), R 5 cmp #1, R 5 jeq rtnval dec R 5 push R 5 call fact ; save state ; get n ; R 5=n-1 ; compute n-1! ECE 3561 - Lecture 1 8

On Recursive call l The stack will look like this l Now what happens

On Recursive call l The stack will look like this l Now what happens when l you start to return with l values? ECE 3561 - Lecture 1 9

Result - on the stack l Result is on the stack and at the

Result - on the stack l Result is on the stack and at the top of the stack. l fact l l l l l l l rtnval cont push mov cmp jjeq dec push call inc pop push call pop jmp mov pop pop ret SR R 5 R 6 R 7 10(SP), R 5 #1, R 5 rtnval R 5 fact R 5 R 6 shmult R 7 cont #1, R 7, 10(SP) R 7 R 6 R 5 SR ; save state ; get n ; R 5=n-1 ; computer n-1! ; R 5 now n ; n-1 fact to R 6 ; stack has null then result ; put value of n! in rtn location ; clean up things Point size is very small – look at it in word ECE 3561 - Lecture 1 10

Performance of code l Code is an extensive use of stack and implementation of

Performance of code l Code is an extensive use of stack and implementation of good coding practices. Passing arguments on stack l Routines have no side effects l ECE 3561 - Lecture 1 11

Extending to 32 bit result l Factorial when result limited to 16 bits does

Extending to 32 bit result l Factorial when result limited to 16 bits does not allow for much range for factorial. l l l 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 In a 16 -bit value 8! Is the largest that can be computed. Could 32 -bits be used? ECE 3561 - Lecture 1 12

A look at the math l The math ECE 3561 - Lecture 1 13

A look at the math l The math ECE 3561 - Lecture 1 13