.z80 ;use z80 mode ; ;********************************************* ;* Small Example using STRU * ;* by Rolf-Dieter Klein 821229 1.0 * ;********************************************* ; a small pocket calculator ; ; jp start ;start main program ; bdos equ 5 ;cp/m entry point ; procedures for the calculator ; ci: ;get byte from console to akku push hl ;save all registers push de push bc ld c,1 ;ci function call bdos pop bc pop de pop hl ret ;result in akku co: ;put byte to console push hl ;value in c register push de push bc ld e,c ld c,2 call bdos pop bc pop de pop hl ret ; ; print: ;print a string till 0 ;start adress at -> HL while (hl)<>0 ld c,(hl) call co inc hl endwhile ret ; ; getnum: ;get dezimal or hex number ;$FF or 12333 are valid numbers ;hl is result ;carry means no value is valid ld a,0 ld (flag),a ;no sign ld hl,0 ;initial value call ci ;get first value if a = '-' ;negative sign ld a,1 ld (flag),a call ci ;get next endif if a = '$' ;is it a hex value ? call ci if not a in ['0'..'9','A'..'F','a'..'f'] scf ;error exit no hex number exit endif while a in ['0'..'9','A'..'F','a'..'f'] add hl,hl ;hl * 16 add hl,hl add hl,hl add hl,hl if a in ['A'..'F'] sub 'A' add a,10 else if a in ['a'..'f'] sub 'a' add a,10 else sub '0' endif endif and 0fh ld e,a ld d,0 add hl,de ;result + new digit call ci endwhile else if not a in ['0'..'9'] scf ;error was no digit exit ;leave procedure endif while a in ['0'..'9'] ld d,h ld e,l ;multiply hl with 10 add hl,hl add hl,hl add hl,de ;*5 add hl,hl ;*10 and 0fh ;digit ld e,a ld d,0 add hl,de ;result value call ci endwhile endif push af ;save terminator ld a,(flag) if a>0 ld de,0 ex de,hl xor a sbc hl,de ;complement endif pop af scf ccf ;hl = value ret ;no carry akku=terminator hexout: ;prints hl in hex ld a,h call nibble ld a,l nibble: push af rrca!rrca!rrca!rrca and 0fh call hexdigit pop af and 0fh hexdigit: if a in [10..15] sub 10 add a,'A' else add a,'0' endif ld c,a call co ret ; ; dezout: ;prints hl in dez ld c,0 push bc ;save flag on stack bit 7,h if nz ld c,'-' call co ld de,0 ex de,hl xor a sbc hl,de ;complement if negative endif ; repeat ; hl := hl div 10 ; a := hl mod 10 ld a,0 ;overflow do b,16 ;for each bit sla l rl h rl a cp 10 ;div 10 if nc sub 10 set 0,l endif enddo add a,'0' ld c,a push bc ;save it until h=0 and l=0 pop bc while c<>0 call co pop bc endwhile ret txt1: defm 'Pocket-Hex Calculator' defb 0dh,0ah defm 'Written in STRU by Rolf-D.Klein ' defb 0dh,0ah,0 txt2: defb 0dh,0ah defm 'Enter Formula ->' defb 0 ; start start: ld hl,txt1 call print loop ld hl,txt2 call print call getnum exitif a = 3 ;CTRL-C stops while a in ['+','-'] if a = '+' push hl call getnum exitif a = 3 pop de add hl,de else push hl call getnum exitif a = 3 pop de push af xor a ex de,hl sbc hl,de pop af endif endwhile ld c,' ' call co call hexout ld c,' ' call co call dezout endloop call 0 ;warm boot ; flag: defb 0 ;ram zell end start