Practical No.5,6,7,8
Practical No.5,6,7,8
CLO PLO LL
2 3 3
OBJECTIVE:
To develop an assembly language program that enables user input in the form of characters,
fostering understanding and proficiency in handling input/output operations in low-level
programming environments.
Theory Overview:
Input Operations:
In assembly language programming, taking user input typically involves reading
characters from the standard input device, such as the keyboard.
Input operations are commonly performed using system interruptions or specialized
input/output instructions provided by the CPU or operating system.
User input
The emu8086 assembler supports user input by setting a predefined value 01 or 01H
in the AH register and then calling interrupt (INT). It will take a single character from
the user and save the ASCII value of that character in the AL register. The emu8086
emulator displays all values in hexadecimal.
; input a character from user
MOV AH, 1
INT 21h ; the input will be stored in AL register
Displaying output
The emu8086 supports single character output. It also allows multi-character or string
output. Similar to taking input, we have to provide a predefined value in the AH
register and call interrupt. The predefined value for single character output is 02 or
02H and for string output 09 or 09H. The output value must be stored in the general-
purpose data register before calling interrupt.
; Output a character
MOV AH, 2
MOV DL, 35
INT 21H
; Output a string
Computer Organization and Assembly Language CSC346 Department of Computer Science
MOV AH, 9
LEA DX, output
INT 21H
As shown in the code, for a single character output, we store the value in the DL register
because a character is one byte or 8 bits long. However, for string output it is a bit different.
We must load the effective address (address with offset) of the string variable in the DX
register using LEA instruction. The string variable must be defined in data segment.
ASCII Representation:
Characters entered by the user are typically represented using ASCII (American
Standard Code for Information Interchange) encoding.
ASCII codes represent printable characters, control characters, and special symbols
using numeric values ranging from 0 to 127.
Syntax:
.model small
.stack 100h
.data
.code
main proc
mov ah,1 ; Input character from user
INT 21h
mov dl, al
mov ah,2 ; display output character
INT 21h
mov ah,4ch
INT 21h
main
endp
end main
Tasks
TASK 1: Write an assembly language program prompt a user to input a character and
display on screen with proper message.
TASK 2: Write a Program to input a capital letter from user and convert it into small
letter (uppercase to lowercase).
Computer Organization and Assembly Language CSC346 Department of Computer Science
TASK 3: Write an assembly language Program to input a small letter from user and
convert it into capital letter (lowercase to uppercase).
CONCLUSION:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
RUBRICS:
CLO PLO LL
2 1 2
Objectives:
Overview:
Ax is the accumulator register. It is the register used most commonly when performing
arithmetic operations. When adding or subtracting numbers, the operations are performed in
the accumulator (ax) register. To work with small numbers, al register can be used, which is
the lower 8 bits of the ax register. This allows to work with bits up to 256 (28).Additionally,
working with the complete 16-bit ax register is sufficient.
Addition in Assembly
Subraction
Use the mul operation on the BX register, which will then multiply the 2 operands and store
the result in the AX register.
.code
main proc
mov ax, 10
mov bx,2
mul bx
main endp
Division
TASK 1: Write a program to add two numbers and print the register value using service
routine.
TASK 2: Write a program to subtract two numbers and print the register value using service
routine.
TASK 3: Write a program to multiply two numbers and print the register value using service
routine.
TASK 4: Write a program to divide two numbers and print the register value using service
routine.
CONCLUSION:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
RUBRICS:
CLO PLO LL
2 3 3
Objective:
The emu8086 assembler supports user input by setting a predefined value 01 or 01H in
the AH register and then calling interrupt (INT). It will take a single character from the user
and save the ASCII value of that character in the AL register. The emu8086 emulator displays
all values in hexadecimal.
Syntax:
;input a character from user
MOV AH, 1
INT 21h ; the input will be stored in AL register
Note Add ASCII code of 0=48 to print the integer output.
Tasks
TASK 1: Write a program to get 5 integer values from the user using service routine and print
them on different lines with proper message.
TASK 2: Write a program to get two integer numbers from the user, add those numbers and
display on the screen.
Hint:
For number:
Mov bl, (any number)
Mov cl, (any number)
Task 3: Write an assembly language programs to input values from user and evaluate the
expression A= B+C-D*E
Computer Organization and Assembly Language CSC346 Department of Computer Science
CONCLUSION:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
RUBRICS:
Overview
Flag registers
Used to indicate outcomes of arithmetic and logic operations.
Flags include zero result, carry-over, and overflow conditions.
Guides program flow in conditional branching based on previous operation outcomes.
Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow.
For example when you add bytes 255 + 1 (result is not in range 0...255).
When there is no overflow this flag is set to 0.
Zero Flag (ZF) - set to 1 when result is zero. For none zero result this flag
is set to 0.
Sign Flag (SF) - set to 1 when result is negative. When result is positive it
is set to 0. Actually this flag take the value of the most significant bit.
Parity Flag (PF) - this flag is set to 1 when there is even number of one bits
in result, and to 0 when there is odd number of one bits. Even if result is a
word only 8 low bits are analyzed!
Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low
nibble (4 bits).
Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to
interrupts from external devices.
Computer Organization and Assembly Language CSC346 Department of Computer Science
Direction Flag (DF) - this flag is used by some instructions to process data
chains, when this flag is set to 0 - the processing is done forward, when this
flag is set to 1 the processing is done backward.
Conditions
Conditional execution in assembly language is accomplished by several looping
and branching instructions. These instructions can change the flow of control in
a program. Conditional execution is observed in two scenarios:
SN Conditional Instructions
1 Unconditional jump
This is performed by the JMP instruction. Transfer control to a
different part of the code without conditions.
SYNTAX:
The JMP instruction provides a label name where the flow of control is
transferred immediately. The syntax of the JMP instruction is:
JMP label
2 Conditional jump
This is performed by a set of jump instructions j <condition> depending
upon the condition. The conditional instructions transfer the control by
breaking the sequential flow and they do it by changing the offset value
in IP.
The syntax for the J<condition> set of instructions:
Example,
CMP AL, BL
Computer Organization and Assembly Language CSC346 Department of Computer Science
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: ...
EQUAL: ...
Following are the conditional jump instructions used on unsigned data used for
logical operations:
Instruction Description Flags Tested
JE/JZ Jump Equal or Jump Zero ZF
JNE/JNZ Jump not Equal or Jump Not Zero ZF
Jump Above or Jump Not
JA/JNBE Below/Equal CF, ZF
JAE/JNB Jump Above/Equal or Jump Not CF
Below
JB/JNAE Jump Below or Jump Not CF
Above/Equal
JBE/JNA Jump Below/Equal or Jump Not CF, AF
Above
The following conditional jump instructions have special uses and check the value of flags:
SYNTAX
CMP compares two numeric data fields. The destination operand could be either in register or
in memory. The source operand could be a constant (immediate) data, register or memory
EXAMPLE:
CMP is often used for comparing whether a counter value has reached the number of time a
loop needs to be run. Consider the following typical condition:
INC DX
CMP DX, 10 ; Compares whether the counter has reached 10
JLE LP1 ; If it is less than or equal to 10, then jump to LP1
Label:
A label is an identifier that acts as a place marker for an instruction. It must end with a colon
(:) character. Label can be any character combination but it cannot start with a number, for
example here are 3 legal label definitions:
label1:
label2:
a:
Label can be declared on a separate line or before any other instruction, for example:
x1:
MOV AX, 1
x2:
MOV AX, 2
Computer Organization and Assembly Language CSC346 Department of Computer Science
Program:
Take a character as input from user and find the occurrence of that character in hard coded
string
Program:
.model small
.stack 100h
.data
String db 'helloworld$'
.code
main proc
mov ax,@data
mov ds,ax
mov ah,1 ;Input
INT 21h
mov dl,0 ; using for counting and printing in end
mov bl,'$' ;for comparing the end of string
mov si, offset String
L1:
cmp bl,[si] ; comparing $ and letter of string
je ToEnd
cmp al,[si] ; comparing input letter with letter of string
je Counter
inc si
jmp L1
Counter: add dl,1 ; increment to dl register for counting occurrence
inc si
jmp L1
ToEnd:
add dl,48
Computer Organization and Assembly Language CSC346 Department of Computer Science
mov si, offset String: describes an instruction that moves the offset address of the
memory location where the string variable "String" is stored into the SI (source index)
register.
L1: is a label declaration. It defines a symbolic name "L1" for a specific location in
the code.
cmp bl,[si]: describes an instruction that compares the value in the BL register with
the byte located at the memory address pointed to by the SI register.
je ToEnd: describes a conditional jump instruction
cmp al,[si]: describes an instruction that compares the value in the AL register with
the byte located at the memory address pointed to by the SI register.
je Counter: describes a conditional jump instruction.
inc si: describes an instruction that increments the value stored in the SI (source
index) register by 1.
jmp L1: describes an unconditional jump instruction.
Counter: add dl,1: describes an instruction that adds the immediate value 1 to the
DL register.
inc si: describes an instruction that increments the value stored in the SI (source
index) register by 1.
jmp L1: describes an unconditional jump instruction.
ToEnd: is a label declaration. It defines a symbolic name "ToEnd" for a specific
location in the code.
add dl,48: describes an instruction that adds the immediate value 48 to the DL
register.
mov ah,2: describes an instruction that moves the immediate value 2 into the AH
register.
INT 21h: This instruction calls interrupt 21h again, but this time to terminate the
program, using the function specified in the AH register.
mov ah,4ch: This instruction moves the immediate value 4Ch into the AH register.
This is the DOS function number for program termination.
INT 21h: This instruction calls interrupt 21h again, but this time to terminate the
program, using the function specified in the AH register.
main endp: This line marks the end of the main procedure.
Computer Organization and Assembly Language CSC346 Department of Computer Science
end main: This line marks the end of the program and specifies the entry point of
the program as the main procedure.
TASKS:
TASK 1:
Write a program to get an integer from user and display whether the number is even or
odd.
TASK 2:
Write a program to get an integer from user and display which is divisible by 2 and 3.
TASK 3:
Write a program to input two numbers and check if they are equal, unequal, greater or lesser.
CONCLUSION:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
RUBRICS: