ENEE350 Computer Organization Lecture-8 Return to lecture notes

Branching and Subroutine (Function) Calls in CodeMill

Branch instructions in CodeMill are much more extensive than in Vesp.
         
         
/* Branch Instructions
JMP D,address; go to address
JMP R,address; go to address + (PC)
JMP X,address; go to address + (IX)
JMP T,address; go to the address in the address 
*/
/* >, =, <=, >=, <>
JCD Rx, <, number, label, register id,
address;
 
JCR Rx, <, number, label, register id,
address;
 
JCX Rx, <, number, label, register id,address;
 
JCT Rx, <, number, label, register id,address;
 
JSR subroutine name;
 
RTS; (Return from subroutine)
 
*/
         
Example 1:
         
/* Simple loop to sum the first +ve 10 integers */
 
CLR R0;
LDI R1,1;
STI 10,3;
 
sum: ADD R0,R1;
INC R1;
JCT R1,<=,n, 10;
 
n: 10;
         
Example 2:
         
         /* Subroutine Version of Simple Sum */
/* main */
CLR R0;
LDI R1,1;
JSR sum;
HLT;
 
sum: ADD R0,R1;
INC R1;
JCD R0,<=,n,sum;
RTS;
n: 5;
         
         Example 3:
         
Recursive sums       

Fibonacci Sum: f(n) = f(n-1) + f(n-2); f(2) = f(1) = 1;

Fibonacci in C
int main(void)
{int n = 10;
int fn;
fn = fibonacci(n);
return 0;
}
 
int fibonacci(int n)
{if ( n <= 2) return 1;
else 
return fibonacci(n-1)
+ fibonacci(n -2);
}
         
Example 4:
         
/* Iterative Fibonacci in CodeMill */
 
/* f(n) = f(n-1) + f(n-2);
 
f(2) = f(1) =1;
*/
/* R2 <=> f(n)
R1 <=> f(n-1)
R0 <=> n
R3 <=> temp 
*/
LDI R0,3;
LDI R1,1;
LDI R2,1;
 
fib: MOV R3,R2;
ADD R2,R1;
MOV R1,R3;
INC R0;
JCD R0,<=,n,fib;
 
n: 10;
         
         
         Example 5:
         
         /* Recursive Fibonacci in CodeMill */
/* Fibonacci Sequence */
/* f(n) = f(n-1) + f(n-2)
 
f(2) = f(1) = 1 */
 
/*Recursive version */
 
/*
R2: f(n)
R1: f(n-1)
R0: n */
  
/* Main */
 
LDI R2,1; LDI R1,1;
LDI R3,1; LDI R0,n;
JSR fib;
HLT;
         
fib: JCD R0,<=,2,ret;
DEC R0; PUS R0;
JSR fib;
POP R0; DEC R0;
JSR fib;
MOV R3,R2;
ADD R2,R1;
MOV R1,R3;
RTS;
ret: LDI R1,1; RTS;
n: 5;
         
         
         a: 1; /* Address 1 */
b: 2;
c: 3;
d: 4;
e: 5;
f: 6;
g: 7;
h: 8;
 
PUS h; PUS g;
SUS; 
PUS f; PUS e;
ADS;
ADS;
PUS d; PUS c;
SUS; 
PUS b; PUS a;
ADS;
ADS;

SUS;

Input/Output Programming
 
RCV Rx,0; /* Read key from keyboard into register Rx. */
SND address,color; write the pixel to the address in the VRAM
 
loop: RCV R0,0;
JCD R0,<,0,loop;
JCD R0,=,8,delete;
CHF R1R2,R0,0F;
ADD R1,10; 
JMP D,loop;
delete: SUB R1,10;
CLF R1R2,10;

JMP D,loop;

Return to lecture notes