URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [examples/] [guess_number.as] - Rev 158
Go to most recent revision | Compare with Previous | Blame | View Log
/************************ guess_number.as *********************************** Author: Agner Fog* date created: 2018-02-23* last modified: 2021-08-04* Version: 1.11* Project: ForwardCom example, assembly code* Description: Guessing game. Guess a number 1 - 100** Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses*****************************************************************************/%buffersize = 0x20 // size of input text buffer%maxnum = 100 // maximum numberconst section read ip // read-only data sectionintro: int8 "\nGuessing game. Guess a number 1 - %i\n",0 // format stringguess: int8 "Your guess:",0 // textsmaller: int8 "\nSmaller",0 // textbigger: int8 "\nBigger",0 // textconclude: int8 "\nYou guessed the number %i in %i tries",0 // format stringanother: int8 "\nAnother? (y/n) ",0 // textgoodbye: int8 "\nGoodbye", 0 // textconst endbss section datap uninitialized // uninitialized read/write data sectionint64 parlist[4] // parameter list for printfint8 buf[buffersize] // input bufferbss endcode section execute // code sectionextern _puts: function // library function: write string to stdoutextern _gets_s: function // library function: read string from stdinextern _printf: function // library function: formatted output to stdoutextern _atoi: function // library function: convert string to integerextern _divide_int: function // library function: divide intergers (if core has no division instruction)_main function public // program begins heredo { // loop to repeat gameint64 r0 = address([intro]) // format string to printfint64 r1 = address([parlist]) // parameter listint32 [r1] = maxnum // put maxnum in parameter listcall _printf // printf("\nguess a number 1 - %i\n", maxnum);int32 r0 = read_perf(perf1,1) // read CPU clock counter to use as a random numberuint32 r0 = abs(r0,1) // avoid negative numbers//call _time // or use time as random number// get number modulo maxnum (0 - 99)//uint32 r1 = r0 % maxnum // use this if core has division instructionint32 r1 = maxnumcall _divide_int // this function returns a/b in r0 and a%b in r1int32 r16 = r1 + 1 // number to guess (1 - 100)for (int64 r17 = 1; ; r17++) { // loop to guess number. no exit condition hereint64 r0 = address([guess])call _puts // write "your guess"int64 r0 = address([buf])int64 r1 = buffersizecall _gets_s // read input into bufferint64 r0 = address([buf])call _atoi // convert string to integerif (int32 r0 == r16) {break} // guess is correct. stop 'for' loopif (int32 r16 < r0) { // select text: "smaller" or "bigger"int64 r0 = address([smaller])}else {int64 r0 = address([bigger])}call _puts // write "smaller" or "bigger"}// guessing loop is finishedint64 r0 = address([conclude]) // format string for printfint64 r1 = address([parlist]) // parameter listint32 [r1] = r16 // put guessed number in parameter listint32 [r1+8] = r17 // put number of tries in parameter listcall _printf // printf("\nyou guessed the number %i in %i tries", r16, r17)int64 r0 = address([another])call _puts // write "\nanother? (y/n) "int64 r0 = address([buf])int64 r1 = buffersizecall _gets_s // read answer into bufferint8 r0 = [buf] // get first character of answer, ignore the restint8+ r0 |= 0x20 // convert to lower case}while (int8 r0 == 'y') // repeat 'do' loop if answer is "y"int64 r0 = address([goodbye])call _puts // write "goodbye"int r0 = 0 // program return valuereturn // return from main_main endcode end
Go to most recent revision | Compare with Previous | Blame | View Log
