URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [examples/] [calculator.as] - Rev 158
Go to most recent revision | Compare with Previous | Blame | View Log
/**************************** calculator.as ********************************** Author: Agner Fog* date created: 2021-05-26* last modified: 2021-07-20* Version: 1.11* Project: ForwardCom example, assembly code* Description: Simple test of arithmetic instructions* Link with libc_light.li** Copyright 2021 GNU General Public License http://www.gnu.org/licenses*****************************************************************************/// Library functions in libc_light.liextern _print_string: function reguse=3,0extern _printf: function reguse=0xF,0extern _gets_s: function reguse=3,0extern _atoi: function reguse=3,0extern _multiply_int: function reguse=1,0extern _divide_int: function reguse=3,0%serial_input_status = 9 // serial input status portconst section read ip // read-only data section// text stringsint8 text1 = "\nSimple calculator with two integers, a and b\n\nEnter a: \0"int8 text2 = "\nEnter b: \0"int8 text3 = "\nAgain (y/n)?: \0"int8 text4 = "\nGoodbye\n\0"// format string for printfint8 formatstring = "\n\na = %8i"int8 "\nb = %8i"int8 "\na + b = %8i"int8 "\na - b = %8i"int8 "\na * b = %8i"int8 "\na / b = %8i"int8 "\na %% b = %8i\n\0"const endcode section execute // code section_main function public // program start%stackframe = 64 // size of local dataint64 sp -= stackframe // allocate input buffer on stackdo { // repeat as long as user answers yesint r0 = 1 // clear input bufferint output(r0, r0, serial_input_status)int64 r0 = address([text1])call _print_string // print intro textint64 r0 = spint r1 = stackframe // max. size of input buffercall _gets_s // read a as stringcall _atoi // convert to integerint32 r8 = r0 // save aint64 r0 = address([text2])call _print_string // print Enter bint64 r0 = spint r1 = stackframe // max. size of input buffercall _gets_s // read b as stringcall _atoi // convert to integerint32 r9 = r0 // save b// set up parameter list with results// (reuse the input buffer as parameter list)int32 [sp+0x00] = r8 // aint32 [sp+0x08] = r9 // bint32 r2 = r8 + r9 // a + bint32 [sp+0x10] = r2int32 r2 = r8 - r9 // a - bint32 [sp+0x18] = r2int32 r0 = r8int32 r1 = r9call _multiply_int // a * b, using function callint32 [sp+0x20] = r0int32 r0 = r8int32 r1 = r9call _divide_int // a / b, using function callint32 [sp+0x28] = r0int32 [sp+0x30] = r1 // a % b// print resultsint64 r0 = address([formatstring]) // pointer to format stringint64 r1 = sp // pointer to parameter listcall _printf // print results// ask if the user wants to try againint64 r0 = address([text3])call _print_string // print Again?int64 r0 = spint r1 = stackframe // max. size of input buffercall _gets_s // read answer as stringint8 r1 = [sp] | 0x20 // read first character of answer, convert to lower case} while (int8+ r1 == 'y') // repeat if user enters 'y'// write goodbye textint64 r0 = address([text4])call _print_string // print goodbyeint64 sp += stackframe // release stack frameint r0 = 0return // return from main_main endcode end
Go to most recent revision | Compare with Previous | Blame | View Log
