Unfortunately, the program you have suggested is not simple to convert to assembly for a number of reasons. Text input is comparatively difficult; you need to create an input loop that polls the keypad, updating a buffer in memory when input is received and displaying the entered text on the display as you go. Once that's done, you need to convert from a text string into a number, which has to be done manually. Now you've done that, to square the value you need to multiply A by itself, not an easy task as the Z80 can't multiply or divide natively. Finally you need to convert the number back into a string to display it.
A simplified version of the program may be this:
Code: Select all
CurrentValue = saveSScreen ; saveSScreen = 768 bytes of free memory.
Main:
bcall(_ClrLcdFull) ; Clear the LCD.
xor a ; a eor a = 0
ld (CurrentValue),a ; Set "CurrentValue" to a, ie 0.
ProgramLoop:
; In our loop, first thing we need to do is display the running status.
xor a \ ld (CurCol),a ; Set column to 0.
xor a \ ld (CurRow),a ; Set row to 0.
ld hl,VariablePrompt ; HL = pointer to "Variable=?" prompt string.
bcall(_PutS)
ld a,(CurrentValue) ; Load the CurrentValue into A.
ld h,0 ; Copy the CurrentValue into HL.
ld l,a ;
bcall(_DispHL) ; Display the value of HL.
xor a \ ld (CurCol),a ; Set column to 0.
inc a \ ld (CurRow),a ; Set row to 1.
ld hl,AnswerPrompt ; HL = pointer to "Variable²=" string.
bcall(_PutS)
ld a,(CurrentValue) ; Load the CurrentValue into A.
ld b,a ; Copy the CurrentValue into B.
call SquareB ; Compute B² (result ends up in HL).
bcall(_DispHL) ; Display the value of HL.
bcall(_NewLine) ; Move cursor onto next line.
WaitKeyLoop:
bcall(_GetCSC) ; Get a key.
or a ; Is it 0?
jr z,WaitKeyLoop ; Yes, so jump back to WaitKeyLoop.
cp skClear ; Is it Clear?
ret z ; If so, quit the program.
ld hl,CurrentValue ; HL is a pointer to the current value.
cp skUp ; Is it Up?
jr nz,NotUp ; If not, jump to NotUp
inc (hl) ; If we get here (ie, Up was pressed) increment the value at HL.
jr ProgramLoop ; Jump back to the main program loop.
NotUp:
cp skDown ; Is it Down?
jr nz,NotDown ; If not, jump to NotDown
dec (hl) ; If we get here (ie, Down was pressed) decrement the value at HL.
jr ProgramLoop ; Jump back to the main program loop.
NotDown:
jr WaitKeyLoop ; Loop back to WaitKeyLoop
SquareB: ; Routine that computes B² and stores the value in HL.
; This is a very naïve implementation!
; It squares A by adding it to HL B times.
ld hl,0 ; Running total.
; First: is B zero? If so, quit.
ld a,b ; Copy B to A.
or a ; If A was 0, Z flag is set.
ret z ; Exit routine if Z flag is set.
ld d,0 ; Set DE to = B.
ld e,b ;
SquareBLoop:
add hl,de
djnz SquareBLoop ; Loop around to SquareBLoop until B = 0
ret ; We're done!
VariablePrompt: .db "Variable=? ", 0
AnswerPrompt: .db "Variable", LSquare, "= ", 0
Rather than prompt for text input, it only lets the user press up or down to increment or decrement a value. It is also limited to handling 8-bit integral input values.