Example - 07textio.asm - Text I/O Procedures |
; -------------------------------------------------------------- ; A program to read in a string of text and store it in RAM. ; The end of text will be labelled with ASCII code zero/null. ; -------------------------------------------------------------- ; THE MAIN PROGRAM MOV BL,70 ; [70] is the address where the text will ; be stored. The procedure uses this. CALL 10 ; The procedure at [10] reads in text and ; places it starting from the address ; in BL. ; BL should still contain [70] here. CALL 40 ; This procedure does nothing until you ; write it. It should display the text. HALT ; DON'T USE END HERE BY MISTAKE. ; -------------------------------------------------------------- ; A PROCEDURE TO READ IN THE TEXT ORG 10 ; Code starts from address [10] PUSH AL ; Save AL onto the stack PUSH BL ; Save BL onto the stack PUSHF ; Save the CPU flags onto the stack Rep: IN 00 ; Input from port 00 (keyboard) CMP AL,0D ; Was key press the Enter key? JZ Stop ; If yes then jump to Stop MOV [BL],AL ; Copy keypress to RAM at position [BL] INC BL ; BL points to the next location. JMP Rep ; Jump back to get the next character Stop: MOV AL,0 ; This is the NULL end marker MOV [BL],AL ; Copy NULL character to this position. POPF ; Restore flags from the stack POP BL ; Restore BL from the stack POP AL ; Restore AL from the stack RET ; Return from the procedure. ; -------------------------------------------------------------- ; A PROCEDURE TO DISPLAY TEXT ON THE SIMULATED SCREEN ORG 40 ; Code starts from address [10] ; **** YOU MUST FILL THIS GAP **** RET ; At present this procedure does ; nothing other than return. ; -------------------------------------------------------------- END ; It is correct to use END at the end. ; -------------------------------------------------------------- TASK 17) Write a program using three procedures. The first should read text from the keyboard and store it in RAM. The second should convert any upper case characters in the stored text to lower case. The third should display the text on the VDU screen. ; -------------------------------------------------------------- |
You can copy this example program from the help page and paste it into the source code editor.
The BL register contains 70. This value is needed by the text input procedure. It is the address where the text will be stored in RAM. This is an example of passing a parameter using a register. All you are doing is getting a number from one part of a program to another.
This command adds one to BL. The effect is to make BL point to the next memory location ready for the next text character to be stored.
Call the procedure at address [10]. This is achieved in practice by setting the CPU instruction pointer IP to [10].
At the end of the procedure, the RET command resets the CPU instruction pointer IP back to the instruction after the CALL instruction to the procedure. This address was stored on the stack by the CALL instruction.
Don't confuse HALT and END. The END command causes the assembler to stop scanning for more instructions in the program. The HALT command generates machine code 00 which causes the CPU to halt. There can be several HALT commands in a program but only one END command.
Origin [10]. The assembler program starts generating machine code from address [10].
Save the value of AL onto the stack. This is an area of RAM starting at address BF. The stack grows towards zero. The RAM displays show the stack pointer as a blue highlight with yellow text. Push and Pop are used so that procedures and interrupts can tidy up after themselves. The procedure or interrupt can alter CPU registers but it restores them to their old values before returning.
PUSHF saves the CPU flags onto the stack. POPF restores the CPU flags to their original value. This enables procedures and interrupts to do useful work without unexpected side affects on the rest of the program.
Input from port zero. This port is connected to the keyboard. The key press is stored into the AL register.
Compare the AL register with the hexadecimal number 0D. 0D is the ASCII code of the Enter key. This line is asking "Was the enter key pressed?" CMP works by subtracting 0D from AL. If they were equal then the subtraction gives an answer of zero. This causes the CPU zero or 'Z' flag to be set.
Jump to the Stop label if the CPU 'Z' flag was set. This is a conditional jump.
Move the key press stored in AL into the RAM location that [BL] points to. INC BL is then used to make BL point to the next RAM location.
Jump back to the Rep label. This is an unconditional jump. It always jumps and the CPU flags are ignored.
Return from the procedure to the address stored on the stack. This is done by setting the instruction pointer IP in the CPU.
© C Neil Bauers 2003