0% found this document useful (0 votes)
49 views16 pages

Practical No.5,6,7,8

Practice question for assembly language

Uploaded by

jawadmalik6467
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views16 pages

Practical No.5,6,7,8

Practice question for assembly language

Uploaded by

jawadmalik6467
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Computer Organization and Assembly Language CSC346 Department of Computer Science

Practical No. 5: Input/Output in the form of characters.

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:

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 5
Conduct Structure
practical
5 5
Data Analysis &
Efficiency
Interpretation

Total Marks obtained Total Marks Obtained

Instructors Signature: ________________________


Computer Organization and Assembly Language CSC346 Department of Computer Science

Practical No.6: Add, subtract, multiply and divide numbers in


assembly.

CLO PLO LL
2 1 2
Objectives:

The objective is to perform basic arithmetic operations, including addition, subtraction,


multiplication, and division, on numbers using assembly language. Specifically, these
operations are carried out using the accumulator (ax) register for addition and subtraction,
and both the ax and bx registers for multiplication and division. The goal is to efficiently
manipulate numerical data at the low-level assembly language, facilitating mathematical
computations in a computer program or system.

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

Add operation is used to perform addition in x86 assembly.


This is shown below.
main proc
mov ax, 10
add ax,2
main endp
end main
Add operations can be performed more than one time.
This is shown in the code below.
main proc
mov ax, 10
add ax,2
add ax,1
main endp
Computer Organization and Assembly Language CSC346 Department of Computer Science

Subraction

The sub operation allows to subtract in x86 assembly.


This is shown below.
main proc
mov ax, 10
sub ax,2
main endp
Multiplication

Multiplication and division is different than addition or subtraction.


Unlike addition and subtraction, multiplication and division require an additional register
other than the AX register.
Place one operand in the AX register.
Place the other operand in the BX register.

Use the mul operation on the BX register, which will then multiply the 2 operands and store
the result in the AX register.

Multiply 10 and 2 and get an output of 14 in hex or 20 in decimal.

.code
main proc
mov ax, 10
mov bx,2
mul bx
main endp
Division

For division, place one operand, the quotient, in the AX register.


Place the other operand, the divisor, in the BX register.
Use the div operation on the BX register, which will divide the quotient by the divisor and
store the result in the AX register.
In this code, divide 8 by 2 and get an output of 4.
main proc
mov ax, 8
mov bx,2
div bx
main endp
Computer Organization and Assembly Language CSC346 Department of Computer Science

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:

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 5
Conduct Structure
practical
5 5
Data Analysis &
Efficiency
Interpretation

Total Marks obtained Total Marks Obtained

Instructors Signature: ________________________

Practical No.7: Get Input from User and perform arithmetic


operations
Computer Organization and Assembly Language CSC346 Department of Computer Science

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:

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 5
Conduct Structure
practical
5 5
Data Analysis &
Efficiency
Interpretation

Total Marks obtained Total Marks Obtained

Instructors Signature: ________________________


Computer Organization and Assembly Language CSC346 Department of Computer Science

Practical No. 8: Flag Registers and jumps (conditional and


unconditional)
CLO PLO LL
3 4 4
OBJECTIVE:
Flag registers are fundamental in indicating the outcome of arithmetic and logic operations,
encoding information such as zero results, carry-over, and overflow conditions. These flags
help the processor make decisions based on the outcomes of previous operations, guiding
program flow in conditional branching. Jumps, both conditional and unconditional, are
instructions that alter the flow of program execution.

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.

 Overflow Flag (OF) - set to 1 when there is a signed overflow. For


example, when you add bytes 100 + 50 (result is not in range -128...127).

 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:

Instruction Description Flags tested


JXCZ Jump if CX is Zero none
JC Jump If Carry CF
JNC Jump If No Carry CF
JO Jump If Overflow OF
JNO Jump If No Overflow OF
JP/JPE Jump Parity or Jump Parity Even PF
Computer Organization and Assembly Language CSC346 Department of Computer Science

JNP/JPO Jump No Parity or Jump Parity Odd PF


JS Jump Sign (negative value) SF
JNS Jump No Sign (positive value) SF

The CMP Instruction


The CMP instruction compares two operands. It is generally used in conditional execution.
This instruction basically subtracts one operand from the other for comparing whether the
operands are equal or not. It does not disturb the destination or source operands. It is used
along with the conditional jump instruction for decision making.

SYNTAX

CMP destination, source

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 DX, 00 ; Compare the DX value with zero


JE L7 ; If yes, then jump to label L7
.
.
L7:
……..

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 ah,2 ; printing the counter


INT 21h
mov ah,4ch
INT 21h
main endp
end main

Let's go through the provided assembly code line by line:


 .model small: This directive tells the assembler to generate code for a small
memory model. In this memory model, the code, data, and stack segments are
limited to 64 KB each.
 .stack 100h: This directive sets the size of the stack segment to 256 bytes (100h in
hexadecimal).
 .data: This directive starts the data segment, where variables and constants are
declared.
 String db 'helloworld$': describes the declaration of a string variable named
"String".
 .code: This directive starts the code segment, where the program's instructions are
placed.
 main proc: This line declares the beginning of the main procedure or function.
 mov ax,@data: is a directive used to load the segment address of the data segment
into the ax register. mov is the move instruction, which copies the value from one
location to another.
 mov ds,ax: is used to set the value of the data segment register (ds) to the value stored
in the ax register.
 mov ah,1: this is an instruction that moves the immediate value 1 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 dl,0: this is an instruction that moves the immediate value 0 into the DL register.
 mov bl,'$': describes an instruction that moves the ASCII value of the dollar sign ('$')
into the BL register.
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:

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to 5 5
Conduct Structure
practical
5 5
Data Analysis &
Efficiency
Interpretation

Total Marks obtained Total Marks Obtained

Instructors Signature: ________________________

You might also like