OpenCores
URL https://opencores.org/ocsvn/forwardcom/forwardcom/trunk

Subversion Repositories forwardcom

[/] [forwardcom/] [examples/] [calculator.as] - Blame information for rev 153

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 132 Agner
/****************************  calculator.as  *********************************
2
* Author:        Agner Fog
3
* date created:  2021-05-26
4
* last modified: 2021-07-20
5
* Version:       1.11
6
* Project:       ForwardCom example, assembly code
7
* Description:   Simple test of arithmetic instructions
8
*                Link with libc_light.li
9
*
10
* Copyright 2021 GNU General Public License http://www.gnu.org/licenses
11
*****************************************************************************/
12
 
13
// Library functions in libc_light.li
14
extern _print_string:  function reguse=3,0
15
extern _printf:        function reguse=0xF,0
16
extern _gets_s:        function reguse=3,0
17
extern _atoi:          function reguse=3,0
18
extern _multiply_int:  function reguse=1,0
19
extern _divide_int:    function reguse=3,0
20
 
21
%serial_input_status = 9               // serial input status port
22
 
23
const section read ip                  // read-only data section
24
 
25
// text strings
26
int8 text1 = "\nSimple calculator with two integers, a and b\n\nEnter a: \0"
27
int8 text2 = "\nEnter b: \0"
28
int8 text3 = "\nAgain (y/n)?: \0"
29
int8 text4 = "\nGoodbye\n\0"
30
 
31
// format string for printf
32
int8 formatstring = "\n\na     = %8i"
33
int8                  "\nb     = %8i"
34
int8                  "\na + b = %8i"
35
int8                  "\na - b = %8i"
36
int8                  "\na * b = %8i"
37
int8                  "\na / b = %8i"
38
int8                  "\na %% b = %8i\n\0"
39
 
40
const end
41
 
42
 
43
code section execute                   // code section
44
 
45
_main function public                  // program start
46
 
47
%stackframe = 64                       // size of local data
48
int64 sp -= stackframe                 // allocate input buffer on stack
49
 
50
do {                                   // repeat as long as user answers yes
51
 
52
    int   r0 = 1                       // clear input buffer
53
    int   output(r0, r0, serial_input_status)
54
 
55
    int64 r0 = address([text1])
56
    call  _print_string                // print intro text
57
    int64 r0 = sp
58
    int   r1 = stackframe              // max. size of input buffer
59
    call  _gets_s                      // read a as string
60
    call  _atoi                        // convert to integer
61
    int32 r8 = r0                      // save a
62
 
63
    int64 r0 = address([text2])
64
    call  _print_string                // print Enter b
65
    int64 r0 = sp
66
    int   r1 = stackframe              // max. size of input buffer
67
    call  _gets_s                      // read b as string
68
    call  _atoi                        // convert to integer
69
    int32 r9 = r0                      // save b
70
 
71
    // set up parameter list with results
72
    // (reuse the input buffer as parameter list)
73
    int32 [sp+0x00] = r8               // a
74
    int32 [sp+0x08] = r9               // b
75
    int32 r2 = r8 + r9                 // a + b
76
    int32 [sp+0x10] = r2
77
    int32 r2 = r8 - r9                 // a - b
78
    int32 [sp+0x18] = r2
79
    int32 r0 = r8
80
    int32 r1 = r9
81
    call  _multiply_int                // a * b, using function call
82
    int32 [sp+0x20] = r0
83
    int32 r0 = r8
84
    int32 r1 = r9
85
    call  _divide_int                  // a / b, using function call
86
    int32 [sp+0x28] = r0
87
    int32 [sp+0x30] = r1               // a % b
88
 
89
    // print results
90
    int64 r0 = address([formatstring]) // pointer to format string
91
    int64 r1 = sp                      // pointer to parameter list
92
    call  _printf                      // print results
93
 
94
    // ask if the user wants to try again
95
    int64 r0 = address([text3])
96
    call  _print_string                // print Again?
97
    int64 r0 = sp
98
    int   r1 = stackframe              // max. size of input buffer
99
    call  _gets_s                      // read answer as string
100
    int8  r1 = [sp] | 0x20             // read first character of answer, convert to lower case
101
}   while (int8+ r1 == 'y')            // repeat if user enters 'y'
102
 
103
// write goodbye text
104
int64 r0 = address([text4])
105
call  _print_string                    // print goodbye
106
 
107
int64 sp += stackframe                 // release stack frame
108
 
109
int r0 = 0
110
return                                 // return from main
111
 
112
_main end
113
 
114
code end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.