#include <fcntl.h>
#include "vm_instr.h"
Defines | |
#define | VM_DEMO_2_TEXT_ADDR 0x00000000 |
virtual machine factorial demonstration program Assembly code of the factorial demonstration program | |
#define | VM_DEMO_2_DATA_ADDR 0x00001000 |
data area of the demonstration program | |
#define | VM_DEMO_2_STACK_ADDR 0x00001100 |
This demonstration program calculates the factorial of 5.
|
data area of the demonstration program load address for the data area |
|
start of the stack area |
|
virtual machine factorial demonstration program Assembly code of the factorial demonstration program
start: 0x00000000 push_b #0x05 ; parameter for fact(n): n=5 0x00000002 jsr fact ; call subroutine fact(n) 0x00000007 halt ; stop processing fact: 0x00000008 dec_fp #0x0004 ; we got one parameter, adjust fp 0x0000000B ld_i #0x0000,fp ; load first parameter 0x0000000E ldc_i_1 ; load constant 1 ontop or the stack 0x0000000F cmp_i ; compare integer: n == 1 0x00000010 bne fact_rec ; branch if n != 1 0x00000013 ld_i #0x0000,fp ; load first parameter 0x00000016 ret_i ; return calculated integer value fact_rec: 0x00000017 ld_i #0x0000,fp ; load first parameter 0x0000001A ldc_i_1 ; load constant 1 ontop or the stack 0x0000001B sub_i ; substract integer: n-1 0x0000001C jsr fact ; call subroutine fact(n-1) 0x00000021 ld_i #0x0000,fp ; load first parameter 0x00000024 mul_i ; multiply result with first parameter 0x00000025 ret_i ; return calculated integer value
load address for the instruction codes |