0% found this document useful (0 votes)
27 views11 pages

Lab Session 3

Uploaded by

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

Lab Session 3

Uploaded by

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

LAB SESSION 3

"Emu8086 Tutorial: Simulating Intel 8086 Processor and Assembly Language


Programming for Addition of Two Numbers"

Lab Questions

o Explain the difference between .model small and .stack 100h.


Answer:

.model small:

 Memory Model: The .model small directive specifies a "small" memory model for the program. This
means that both code (instructions) and data (variables) will fit within a single segment each.
 Segment Usage: In the small model, only two segments are used:
o Code Segment (CS): Stores all executable code (procedures, loops, etc.).
o Data Segment (DS): Stores all global data (variables).
 Addressing: Using a single segment for code and a single segment for data makes addressing simpler but
limits the total size of each segment. In real mode, each segment can only be 64KB in size, so a .model
small program can only have up to 64KB of code and 64KB of data.
 Typical Use: The small model is ideal for simpler programs that don’t need large amounts of code or data.

.stack 100h:

 Stack Definition: The .stack 100h directive sets up a stack segment in memory and allocates 256 bytes
(100h in hexadecimal) for the stack.
 Purpose of the Stack: The stack is a Last In, First Out (LIFO) structure used to store temporary data like
function return addresses, register values during subroutine calls, and local variables.
 Size: Here, 100h (or 256 bytes) is typically enough for small programs. If the program has many nested
calls or requires more temporary storage, a larger stack size might be necessary.
 Location in Memory: When .stack is declared, the assembler sets aside the specified amount of memory
at the start of the program's stack segment, ensuring that there is reserved space for stack operations.

o What is the function of int 21h?


Answer:
The int 21h interrupt is a DOS interrupt used for performing a wide range of DOS
services, such as input/output, file handling, and memory management. By setting
different values in the AH register before calling int 21h, you can invoke various DOS
functions.

INT 21H functions


Function number Description
01h Keyboard input with echo:
e.g. mov ah,01h This operation accepts a character from the
int 21h keyboard buffer. If none is present, waits for
keyboard entry. It returns the character in AL.
02h Display character:
e.g. mov ah,02h Send the character in DL to the standard output
int 21h device console.
09h String output:
e.g. mov ah,09h Send a string of characters to the standard
int 21h output. DX contains the offset address of string.
The string must be terminated with a ‘$’ sign.
0Ah String input
4Ch Terminate the current program
e.g. mov ax,4C00h (mov ah,4Ch
int 21h int 21h is also used.)

o How does the program convert ASCII to numeric values and vice versa?
Answer:
 Converting ASCII to Numeric Value
When the user inputs a digit from 0 to 9, the ASCII code for that digit is stored in the AL
register. ASCII codes for digits start at 30h (48 in decimal) for '0' and go up to 39h (57 in
decimal) for '9'. To convert this ASCII code to its corresponding numeric value, we
subtract 30h (or 48 decimal).
 Converting Numeric Value Back to ASCII
To display the result, the numeric value needs to be converted back to an ASCII character,
as int 21h requires ASCII codes for display purposes. To convert a numeric value (0–9)
back to ASCII, we add 30h (or 48 in decimal) to it.

o What is the purpose of mov ah, 01h and mov ah, 02h?
Answer:
 mov ah, 01h (Input function): Reads a character from the keyboard and places it in
AL.
 mov ah, 02h (Output function): Displays the character in DL on the screen.

o Explain the difference between mov dl, 10 and mov dl, 13.
Answer:
1. mov dl, 10: Newline (Line Feed)
10 in decimal (or 0Ah in hexadecimal) represents the newline (line feed) character in
ASCII. When this character is printed, it moves the cursor down to the next line but does
not return it to the beginning of the line.
This instruction creates a new line on the screen, moving the cursor vertically.
2. mov dl, 13: Carriage Return
13 in decimal (or 0Dh in hexadecimal) represents the carriage return character in ASCII.
When this character is printed, it returns the cursor to the beginning of the current line
without moving it down to the next line.
This instruction resets the cursor’s position to the start of the line, typically used in
conjunction with the newline to start a new line from the beginning.
o Write a short note on the importance of register usage in assembly language
programming.
Answer:
In assembly language programming, registers play a critical role in executing
instructions efficiently and managing data within the CPU. Registers are small, fast
storage locations directly within the processor, which can quickly hold and manipulate
data without needing to access slower memory locations. Here are some key points
highlighting the importance of registers in assembly language:

 Speed and Efficiency: Registers enable the CPU to access and process data
much faster than accessing memory, making programs run more efficiently.
Since instructions using registers execute faster than those involving memory,
optimizing register usage can significantly improve performance.

 Data Manipulation: Registers are essential for performing arithmetic and


logical operations. They act as intermediate storage for data being processed,
such as storing the results of calculations or temporarily holding values during
computations.

 Instruction Requirements: Many assembly instructions require data to be in


specific registers. For instance, interrupt service routines in x86 assembly
expect certain registers to contain specific values. Efficient use of registers
ensures that the program meets these requirements without unnecessary data
movement.

 Minimizing Memory Access: By keeping frequently used data in registers, the


need to frequently access memory is reduced, decreasing memory latency and
improving overall program efficiency. This is particularly important in time-
critical applications where every cycle counts.

 Stack Operations and Parameter Passing: Registers often hold parameters


for functions, return values, and address pointers for stack operations, enabling
efficient function calling and return mechanisms. Using registers to manage
these aspects improves code readability and control flow.

In summary, effective register usage is essential in assembly programming to achieve


high performance, control, and efficient memory usage, making it a vital skill for low-
level programmers.
Additional Exercises:

1. Modify the program to accept multi-digit numbers.


Source Code:
org 100h
.model small
.stack 100h
.data

RANGE DB 13,10,'Enter range $'


R DW ?
EN DB 13,10,'Enter a number $'
N DW ?
UN DB 13,10,'You Entered: $'
SUM DW 0

.CODE

MAIN PROC
mov dx , offset RANGE ;for printing string
mov AH,9
int 21h

mov AH,1
int 21h
mov AH,0
mov R,AX
SUB R,48

mov CX,R

L:
PUSH CX

mov dx , offset EN ;for printing string


mov AH,9
int 21h

mov N,0 ;N=0


mov BL,10 ;BL=10
INPUT: ;LABEL
mov AH,1 ;Input a character => stores in AL=2 e.g (input 2)
int 21h ;interupt
CMP AL,13 ;compare input with enter key
JE NEXT ;if jump equal to then it jumps on label next
SUB AL,30h ;sub AL=AL-48=AL-30h
mov AH,0 ;AH=0
mov CX,AX ;CX=AX => CX=2
mov AX,N ;AX=N => AX=0
mul BL ;multiply => AX = BL*AX = 10*0=0
ADD AX,CX ;Add => AX=AX +CX =0+2 =2
mov N,AX ;N=AX => N=2
JMP INPUT

NEXT:
MOV BX ,N
ADD SUM,BX
POP CX
LOOP L

LEA DX,UN
mov AH,9
int 21h

;output code
mov AX,SUM ;AX=N=>25

mov DX,0 ;DX=0


MOV BX,10 ;BX=10
mov CX,0 ;CX=0 => counter loop

L1:
div bx ;divide => AX=AX/BX =25/10
;incase of 8bit reg => AL=quotient , AH = remainder
;incase of 16bit reg => AX=quotient , DX = remainder
;AX =2, DX=5
push dx ;5 save in stack
mov dx,0 ;DX =0
mov ah,0 ;AX AH 00000000 AL=quotient
inc cx
cmp ax,0 ;(2==0)
jne L1 ;jump not equal => (AX !=0)
mov ah,2 ;output character

L2:
pop dx ;first time pop 2 and second time pop 5 =25
add dx,48
int 21h
loop L2

ret
OUTPUT:

Constraint:
Can only take 4 digits.

2. Implement subtraction, multiplication, and division operations.


Subtraction:

include 'emu8086.inc'
.model small
.stack 100h

.CODE

_MainProc PROC

print 'enter first number:'

mov ah, 01h


int 21h

sub al, 48
mov bl, al

mov dl, 10
mov ah, 02h
int 21h

mov dl, 13
mov ah, 02h
int 21h

print 'enter second number:'

mov ah, 01h


int 21h

sub al, 48

sub bl, al

add bl, 48

mov dl, 10
mov ah, 02h
int 21h

mov dl, 13
mov ah, 02h
int 21h

print 'your Difference is:'

mov dl, bl
mov ah, 02h
int 21h

_MainProc ENDP

END
Multiplication:

include 'emu8086.inc'

.model small
.stack 100h

.CODE

_MainProc PROC

print 'enter first number:'

mov ah, 01h ; AH = 01h for input


int 21h ; Interrupt 21h for input/output operations

sub al, 48 ; Subtract 48 from AL to get numeric value


mov bl, al ; Store the first number in BL register

mov dl, 10 ; DL = 10 for newline


mov ah, 02h ; AH = 02h for output
int 21h ; Print newline

mov dl, 13
mov ah, 02h
int 21h

print 'enter second number:'

mov ah, 01h


int 21h

sub al, 48
mul bl

add al, 48

mov bl,al

mov dl, 10
mov ah, 02h
int 21h

mov dl, 13
mov ah, 02h
int 21h

print 'your Multiplication is:'

mov dl, bl ; Move sum to DL


mov ah, 02h ; AH = 02h for output
int 21h ; Print sum

_MainProc ENDP
END

Division:

3. Display error messages for invalid inputs.


P3-PLO3 Lab Rubrics for Assembly Programming

Criteria Excellent (5) Good (4) Satisfactory (3) Needs Improvement (2) Unsatisfactory (1) Marks

Clearly identifies all


Identifies most
problem requirements, Basic problem Misidentifies or
problem requirements,
Problem Identification & correctly identification with misinterprets some Fails to identify or analyze
with minor gaps in
Analysis interprets task, and breaks limited analysis of components of the the problem correctly.
analysis or
it down into smaller requirements. problem.
understanding.
components for analysis.

Algorithm is mostly
Designs a well-structured
efficient with clear Algorithm is functional Algorithm is incomplete
and efficient algorithm,
Algorithm Design & Logic logic but has some but lacks clarity, or has significant flaws No meaningful algorithm
with clear logic flow and
Flow inefficiencies or minor optimization, or proper in logic and instruction or logic is provided.
correct use of assembly
errors in instruction flow in parts of the code. usage.
instructions.
usage.
Implements the assembly
program correctly with no
Implements the Multiple syntax errors or
syntax errors, Program is functional but
program with minor misuse of instructions and Program is incomplete
demonstrating contains several syntax
Code Implementation & Syntax syntax errors that do addressing modes affect or filled with errors,
complete understanding of errors or improper use of
not affect overall the program’s making it non-functional.
registers, memory registers and addressing.
functionality. functionality.
addressing, and assembly
instructions.

Identifies and resolves


Debugging process is
errors effectively, using Basic debugging is done, Struggles to debug errors, No effective debugging is
mostly effective, with
Debugging and Problem debugging but some errors persist and many remain done; errors prevent
minor issues
Solving tools efficiently to ensure or are solved unresolved in the final program from running
overlooked or not
full functionality of the inefficiently. program. correctly.
resolved optimally.
program.

Provides thorough, clear,


Provides adequate
and well-organized Comments and Code is poorly No documentation or
documentation with
comments documentation are documented, with comments are provided,
Code Documentation & Clarity comments explaining
explaining each section of minimal and do not fully unclear explanations or making the code difficult to
most of the key parts of
the code and key decisions. explain the logic. insufficient comments. understand.
the program.
Code is easy to follow.

You might also like