Software Requirement: emu8086/DosBox0.74
Necessary
Information:
General purpose registers are used to
store temporary data within the microprocessor. There are 8 general purpose
registers in 8086 microprocessors.
Figure – General purpose registers
1. AX – This is the
accumulator. It is of 16 bits and is divided into two 8-bit registers AH and AL
to also perform 8-bit instructions.
It is generally used for arithmetical and logical
instructions but in 8086 microprocessor it is not mandatory to have accumulator
as the destination operand.
Example:
ADD
AX, AX (AX = AX + AX)
2. BX – This is the
base register. It is of 16 bits and is divided into two 8-bit registers BH and
BL to also perform 8-bit instructions.
It is used to store the value of the offset.
Example:
MOV
BL, [500] (BL = 500H)
3. CX – This is the
counter register. It is of 16 bits and is divided into two 8-bit registers CH
and CL to also perform 8-bit instructions.
It is used in looping and rotation.
Example:
MOV
CX, 0005
LOOP
4. DX – This is the
data register. It is of 16 bits and is divided into two 8-bit registers DH and
DL to also perform 8-bit instructions.
It is used in multiplication an input/output port addressing.
Example:
MUL
BX (DX, AX = AX * BX)
5. SP – This is the
stack pointer. It is of 16 bits.
It points to the topmost item of the stack. If the stack is empty the stack
pointer will be (FFFE)H. It’s offset address relative to stack segment.
6. BP – This is the
base pointer. It is of 16 bits.
It is primary used in accessing parameters passed by the stack. It’s offset
address relative to stack segment.
7. SI – This is the
source index register. It is of 16 bits.
It is used in the pointer addressing of data and as a source in some string
related operations. Its offset is relative to data segment.
8. DI – This is the
destination index register. It is of 16 bits.
It is used in the pointer addressing of data and as a destination in some
string related operations. Its offset is relative to extra segment.
Refers to a value added to a base
address to produce a second address. For example, if B represents address 100,
then the expression,
B+5
would signify the address 105. The 5 in the
expression is the offset.
Specifying addresses using an offset is called relative addressing
because the resulting address is relative to some other point. Another word
for offset is displacement.
INT
21:
MS-DOS uses INT 21H for its main API
functions which provides a low-level interface to the devices-reading
input from keyboard, writing to terminal, create/read/write files and
directories etc.
MS-DOS uses other interrupts to provide other services. 2oH for terminate
process, 23H for handling CTRL-C, 27H for terminate and stay resident and
so on.
INT 21H is the assembly language mnemonic for the Intel CPU command used
to perform a System call. The memory model specifies
the memory size assigned to each of the different parts or segments of a
program. There exist different memory models for the 8086 proceessor
The
.MODEL Directive
The
memory model directive specifies the size of the memory the program needs.
Based on this directive, the assembler assigns the required amount of memory to
data and code.
Each
one of the segments (stack, data and code), in a program, is called a logical
segment . Depending on the model used, segments may be in one or in
different physical segments.
In
MASM 6.X, segments are declared using the .MODEL directive. This directive is
placed at the very beginning of the program, or after the optional title
directive.
|
MODEL directive |
|
.MODEL memory_model |
TINY
Model:
In the
TINY model both code and data occupy one physical segment. Therefore, all
procedures and variables are by default addressed as NEAR, by pointing at their offsets
in the segment.
On assembling and linking a source file, the tiny model automatically generates
a com file, which is smaller in size than an exe file.
SMALL
Model:
In the
SMALL model all code is placed in one physical segment and all data in another
physical segment.
In this model, all procedures and variables are addressed as NEAR by pointing
to their offsets only.
COMPACT
Model:
In the
COMPACT model, all elements of code (e.g. procedures) are placed into one
physical segment. However, each element of data can be placed by default into
its own physical segment. Consequently, data elements are addressed by pointing
both at the segment and offset addresses.
In this model, all code elements (procedures) are addressed as NEAR and data
elements (variables) are addressed as FAR.
MEDIUM
Model:
The
MEDIUM model is the opposite of the compact model. In this model data elements
are treated as NEAR and code elements are addressed as FAR.
LARGE
Model:
In the
LARGE model both code elements (procedures) and data elements (variables) are
put in different physical segments. Procedures and variables are addressed as
FAR by pointing at both the segment and offset addresses that contain those
elements. However, no data array can have a size that exceeds one physical
segment (i.e. 64 KB).
HUGE
Model:
The
HUGE memory is similar to the LARGE model with the exception that a data array
may have a size that exceeds one physical segment (i.e. 64 KB).
The
following table summarizes the use of models and the number and sizes of
physical segments that are used with each of the models.
|
Size of Code |
Size of Data |
|
|
TINY |
Code + Data < 64KB |
Code + data < 64KB |
|
SMALL |
Less than 64KB |
Less than 64KB |
|
MEDIUM |
Can be more than 64KB |
Less than 64 KB |
|
COMPACT |
Less than 64KB |
Can be more than 64KB |
|
LARGE* |
Can be more than 64K |
Can be more than 64KB |
|
HUGE** |
Can be more than 64K |
Can be more than 64KB |
Table
1: Memory Models
(*) For the LARGE model, the largest arrays size can not exceed 64 KB.(br) (**)For the HUGE model, an array may have a size greater than 64 KB and hence can span more than one physical segment.
Use of
memory models:
The
amount of data that has to be manipulated and code that needs to be written are
the mojor factors in determining the choice of an appropriate model. The
following are guidelines to help chose the right model for a program.
For a
small fast program that operates on small quantities of data, the SMALL or TINY
models are the most suitable ones. These models allow up to 64K of memory (i.e.
one single physical segment), but the executable code is fast since only near
references are used in the calculation of addresses.
The only difference between these two models is that the TINY model generates a
.COM module in which far references cannot be used, whereas the SMALL model
generates a .exe module.
For
very long programs that require more than one code segment and operate on large
amounts of data which would require more than one data segment, the LARGE and
HUGE models are most appropriate.
1) Write a program to program to print users input in assembly language.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV
AH,1 ; INPUT
INT 21H
MOV BL,AL
MOV
AH,2 ; OUTPUT
MOV
DL,BL
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
2) Write a program to program to print users
input in new line in assembly language.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
; INPUT
INT 21H
MOV BL,AL
MOV AH,2
MOV DL,0AH ;10 = 0AH, Check ascii codes,it
gives new line
INT 21H ; new line
MOV DL,0DH ; 13= 0DH, It helps to display
user input straight below.
INT 21H
MOV AH,2
; OUTPUT
MOV DL,BL
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
3) Write a program to print multiple user
input.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
INT 21H
MOV BL,AL
INT 21H
MOV BH,AL
INT 21H
MOV CL,AL
INT 21H
MOV CH,AL
MOV AH,2
MOV DL,0AH
;10 = 0AH, Check ascii codes,it gives new line
INT 21H
MOV DL,0DH ;
13= 0DH, It helps to display user input straight below.
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV DL,BH
INT 21H
MOV DL,CL
INT 21H
MOV DL,CH
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
4) Write a program to add two numbers.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
INT 21H
MOV BL,AL
INT 21H
MOV CL,AL
ADD BL,CL
;BL = BL+CL
MOV AH,
SUB BL,48 ;
Invisible
MOV DL,BL
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
5) Write a Program to subtract two numbers.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
INT 21H
MOV BL,AL
INT 21H
MOV CL,AL
SUB BL,CL
;BL = BL-CL
MOV AH,2
ADD BL,48 ;
Invisible
MOV DL,BL
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
6) Write a program to multiply two numbers.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
;Input function
INT 21H
MOV BL,AL
SUB BL,48
INT 21H
SUB AL,48
MUL BL ;
MULTIPLICATION NUMBER FOR UNSIGN NUMBER
AAM ; ADJUST AFTER MULTIPLICATION MUST USE TO
SHOW THE RESULT IN TWO DIGIT
MOV CX,AX ;
AFTER MULTIPLYING 2 BYTE WE GET A WORD=16BIT. THIS IS STORED IN AX & WE ARE
MOVING IT TO CX
ADD CH,48
ADD CL,48
MOV AH,2
;OUTPUT FUNCTION
MOV DL,CH;
PRINT HIGHER BYTE
INT 21H
MOV DL,CL
;LOWER BYTE
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
7) Write a program to display string at a
new line in assembly language.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'I AM
BANGLADESHI $'
MSG2 DB 'I AM HUMAN
BEING $'
.CODE
MAIN PROC
MOV AX,@DATA ; Initialization of Data
Segment
MOV DS,AX
LEA DX,MSG1 ; Load Effective Address
MOV AH,9
INT 21H
MOV AH,2
; New Line
MOV DL,0AH ;10 = 0AH, Check ascii codes,it
gives new line
INT 21H
MOV DL,0DH ; 13= 0DH, It helps to display
user input straight below.
INT 21H
LEA DX,MSG2 ; Load Effective Address
MOV AH,9
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
8) Write a assembly program case conversion.
A=10, B= 11, C= 12,
D= 13, E= 14, F = 15, G = 16, H = 17, I= 18, J= 19
.MODEL SMALL
.STACK 100H
.DATA
VAR DB ?
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS, AX
MOV AH,1
INT 21H
MOV VAR, AL
MOV AH,2
MOV DL, '1'
INT 21H
MOV AH,2
SUB VAR,17
MOV DL, VAR
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
9)
CMP = compare
JG = jump if greater than
JNG = jump if not
greater than
JGE = jump if greater
than or equal
JE = jump if equal
JNE = jump if not
equal
JMP = jump
INC = increase
DEC = decrease
.MODEL SMALL
.STACK 100H
.DATA
MAIN PROC
CMP BL,BH
JGE L2
L1:
MOV AH,2
MOV DL,CL
INT 21H
JMP EXIT
L2:
MOV AH,2
MOV DL,CL
INT 21H
JMP EXIT
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
Write a program to compare between two
numbers.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'ENTER AN
INPUT: $'
MSG2 DB ' GREATER $'
MSG3 DB ' SMALLER $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
MOV BL,AL
SUB BL,48
CMP BL,5
JL L1
LEA DX,MSG2
MOV AH,9
INT 21H
JMP EXIT
L1:
LEA DX,MSG3
MOV AH,9
INT 21H
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
10) Write a program to print repeat value
until matching specific value.
.MODEL SMALL
.STACK 100H
.DATA
.CODE
MAIN PROC
L:
MOV AH,1
INT 21H ;INPUT
MOV BL,AL
MOV AH,2
MOV DL,BL
INT 21H ;OUTPUT
MOV DL,0DH
INT 21H ;NEWLINE
MOV DL,0AH
INT 21H
CMP BL,97 ; a =
97(specific value)
JE EXIT
JMP L
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
11) Write a program to check number whether
it is in range or out of range.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'ENTER AN
INPUT: $'
MSG2 DB ' IN RANGE $'
MSG3 DB ' OUT OF RANGE $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
MOV BL,AL
SUB BL,48
CMP BL,4
JGE L2
;JUMP IF GREATER THAN OR EQUAL
L1:
LEA DX,MSG3
MOV AH,9
INT 21H
JMP EXIT
L2:
CMP BL,7
JNG L3 ; JUMP IF NOT GREATER THAN
JMP L1
L3:
LEA DX,MSG2
MOV AH,9
INT 21H
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
;WRITE A PROGRAM TO
DISPLAY STH FROM USER INPUT AND DIRECT INPUT.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 3
MSG2 DB ?
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH,2
ADD MSG1,48
MOV DL,MSG1
INT 21H
MOV AH,1
INT 21H
MOV MSG2,AL
MOV AH,2
MOV DL,0AH ;10 = 0AH, Check ascii codes,it
gives new line
INT 21H
MOV DL,0DH ; 13= 0DH, It helps to display
user input straight below.
INT 21H
MOV AH,2
MOV DL,MSG2
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
;WRITE A PROGRAM TO
DISPLAY STH FROM USER INPUT.
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB ?
MSG2 DB ?
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH,1
INT 21H
MOV MSG1,AL
MOV AH,1
INT 21H
MOV MSG2,AL
MOV AH,2
MOV DL,0AH ;10 = 0AH, Check ascii codes,it
gives new line
INT 21H
MOV DL,0DH ; 13= 0DH, It helps to display
user input straight below.
INT 21H
MOV AH,2
MOV DL,MSG1
INT 21H
MOV AH,2
MOV DL,MSG2
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

0 Comments