You are on page 1of 30

Keyboard Processing

with Control flow


Julius Bancud
Character Input

The following instructions wait until a character is


typed using the keyboard and then store the ASCII
character in AL register. Whatever is typed is also
shown on the screen

MOV AL, 01h ; request for character input


INT 21h
Character Input without Echo
The following instructions wait for an input character
from the keyboard and place it in AL register. However,
character is not displayed on screen.

MOV AH, 08h ; request for character input


INT 21h
Sample program that demonstrates the use of
character input
OUTPUT
Lowercase letter converts to uppercase letter
OUTPUT
CMP INSTRUCTION
CMP (Compare) instruction is used to compare character
data, as well as, numeric data fields. The main use of CMP
is in decision making in conjunction with the conditional
jump instructions.
Format:
CMP <register>,<register>
CMP<register>,<immediate-data>
Example:
CMP AL, ‘Y’
CMP DX, 0000h
CONDITIONAL JUMP INSTRCUTIONS
• These are instructions that transfer control depending
on the setting of CMP instruction whether a certain
condition is met or not.
INSTRUCTION DESCRIPTION

JE JUMP IF EQUAL
JNE JUMP IF NOT EQUAL
JL JUMP IF LESS THAN
JLE JUMP IF LESS THAN OR EQUAL TO
JG JUMP IF GREATER THAN
JGE JUMP IF GREATER THAN OR EQUAL TO
JZ JUMP IF ZERO
JNZ JUMP IF NOT ZERO
CONDITIONAL JUMP INSTRUCTIONS

Format:
<conditional jump instruction> <label>
Example:
CMP AL, ‘Y’
JE TAMA
CMP AL, ‘N’
JE MALI
TAMA: MOV AH, 09h
MOV DX, OFFSET ANS1
INT 21h
UNCONDITIONAL JMP
JMP is considered as unconditional jump instruction because it is always executed by the
machine. It does not depend on a condition being true or false.
Format:
JMP <label>
Example:
CMP AL, ‘Y’
JE TAMA
CMP AL, ‘N’
JE MALI
JMP TAPOS
TAMA: MOV AH, 09h
MOV DX, OFFSET ANS1
INT 21h
TAPOS: MOV AX, 4C00H
INT 21h
IF .. ELSE CONSTRUCT
A conditional statement of C language in the form:
if < condition>
{
<statement-1>;
<statement-2>;

<statement-n>;
}
else
{
<statement-1>;
<statement-2>;
<statement-n>;
}
IF .. ELSE CONSTRUCT
Can be implemented by assembly language of the form:
<CMP instruction>
<conditional jmp instruction> <label-1>
<instruction-1>
<instruction-1>

<instruction-n>
< jmp instruction> <label-2>
IF .. ELSE CONSTRUCT
C LANGUAGE ASSEMBLY LANGUAGE
If (ax == 0)
{
cx = cx – ax; CMP AX, 0000h
ax = ax + 1 JNZ ACTION1
} SUB CX, AX
INC AX
JMP NEXT
else
{ ACTION1: SUB CX, 0008h
cx = cx – 8;
} NEXT:
DO .. WHILE CONSTRUCT

A DO.. WHILE lop similar to C language


do
{
<statement-1>;
<statement-2>;
….
<statement-n>;
} while<condition>;
Do… while

Can be implemented by assembly language of the form:


<label>:
<instruction-1>
<instruction-1>

<instruction-n>
<CMP instruction>
<conditional jmp instruction>
Example
C language Assembly Language

ax = 5 MOV AX, 0005h


do HERE: DEC AX
{ INC BX
ax = ax -1; CMP AX, 0000H
bx = bx + 1; JNZ HERE
}while ( ax>=0);
WHILE CONSTRUCT
Similarly, C language while loop:

while<condition>
{
<statement-1>
<statement-2>
….
<statement-n>
}
Is roughly equivalent to an assembly language structure:

<label-1> : <CMP instruction>


<conditional jmp instruction> <label-2>
<instruciton-1>
<instruciton-2>

<instruciton-n>
JMP <label-1>

<label-2> <instruciton-1>
<instruciton-2>

<instruciton-n>
Example
C Language Assembly Language

START: CMP AX, 000Ah


while ( ax < = 10) JGE NEXT
{ ADD AX, BX
ax = ax + bx; INC BX
bx = bx + 1; JMP START
}
NEXT: ADD CX, AX
Output
STRING INPUT

The following instructions wait for the input string,


pressing ENTER key and the input string is stored to a
specified identifier namely x.

MOV AH, 0AH ; request for string input


MOV DX, OFFSET X ; LOAD ADDRESS
INT 21H
STRING INPUT

• But before you apply instructions to accept string,


you have to define the identifier to hold input
string.
• Format:
<identifier> DB <max-size-str>, <current-size>,
<max-size-str> DUP(“$”)
STRING INPUT
WHERE:
<max-size-str> - numeric constant defining the maximum number of
characters minus three (3)
<current size> - declared as question mark (?) meaning undefined. It will
have a value after the input. It holds the number of characters entered
DUP(“$”) - fills the string with dollar symbol.

Example:
NEYM DB 08h, ?, 08h DUP(“$”)
08H 04H v a l e $ 0Dh
0 1 2 3 4 5 6 7

STRING INPUT

In the above example, it means that the maximum size of


string you can enter is 8 for 08h minus 3. thus the required
number of characters to be entered is only 5 instead of 8.
Why only 5 and not 8? You can only put 5 characters
because 08h will be stored in the first memory address. If
your input is ‘vale’, there are 4 characters, 04h will be stored
in the second memory address, character ‘v’ will be stored
in the third location, ‘a’ in fourth and so on and so forth.
After pressing enter key, 0Dh will be stored at the last
memory address.
Displaying the input string

The following instructions display the input string


starting at the third memory address.

MOV AH, 09H ; request for string output


MOV DX, OFFSET X+2
INT 21h
Sample program that demonstrates string input and
displays it again on screen.
Output
Exercise

Construct an assembly language program that will


allow the user to input from 0 to 9 only and will
convert and display its equivalent number in word.

You might also like