How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the 8086 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the 8086 Assembly programming language

Table of Contents

Problem Statement

Obtain a valid   Y   or   N   response from the keyboard. The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated. The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the 8086 Assembly programming language

Source code in the 8086 programming language

	.model small
	.stack 1024
	
        .data
         
        ;no data needed
	
	.code
	
start:
	mov ax,@code
        mov ds,ax
	
	call PRIMM
	BYTE "Exit program and return to MS-DOS? (Y/N)",0
	
	mov ax,0C00h
	int 21h			;flush keyboard buffer

forever:	
	call waitKey
	;returns ASCII code in AL
	
	and AL,11011111b         ;ignore case
	cmp al,"Y"
	jz ReturnToMSDOS
	
	cmp al,"N"
	jz forever       
	;normally this would jump somewhere else but for simplicity it will wait
	;for a yes response.
	jnz forever 
	
ReturnToMSDOS:
	mov ax,0C00h
	int 21h			  ;flush keyboard buffer
	
	mov ax,4C00h
	int 21h			  ;end program
;-------------------------------------------------------------------
;  SUBROUTINES
;-------------------------------------------------------------------
waitKey:
	mov ah,01h
	int 16h
	jz waitKey
	ret
	;waits until a key is pressed.
	;return: 
	; AL = ASCII CODE
	; AH = SCAN CODE (???)
	
;-------------------------------------------------------------------
PrintString:			;Print null-terminated strings
;input: string address = ds:si

	lodsb			;Load a letter
	cmp al,0		;Was that letter the terminator?
	jz PrintString_Done	;Yes? then RET
	call PrintChar		;Print to screen
	jmp PrintString		;Repeat
PrintString_Done:
	ret	
;-------------------------------------------------------------------
PrintChar:
	push ax
		mov ah,0Eh
		int 10h			;print AL to the screen.
	pop ax
	ret
;-------------------------------------------------------------------

PrintSpace:		
	mov al,' '
	jmp PrintChar	;JMP avoids a tail call.
	;ret		;"PrintChar"'s ret will do this for us.
	
;-------------------------------------------------------------------
NewLine:	
	push dx
	push ax
		mov	ah, 02h	
		mov	dl, 13	;CR
		int	21h		
		mov	dl, 10	;LF
		int	21h		
	pop ax
	pop dx
	ret
;-------------------------------------------------------------------
PRIMM:
	pop si	
	push ax
	;get return address in si, this is the source offset for
	;the string that will be printed.
	;String must be null terminated.
	          call PrintString
	pop ax
	push si 
	;PrintString adjusts the return address for us, it is now
	;just after the null terminator. So put it back on the stack.
	ret
	
;-------------------------------------------------------------------

	
	end start


  

You may also check:How to resolve the algorithm Thue-Morse step by step in the BQN programming language
You may also check:How to resolve the algorithm Sierpinski pentagon step by step in the C++ programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Count in factors step by step in the jq programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Tcl programming language