Sei sulla pagina 1di 7

Interrupts, Memory models & Simple Instructions

Here is your Hello world Program

Assembler directive for reserving memory space


in the stack

Defines the data segment, place where constants


& variables are defined

Assembler directive that defines program


instructions

Can be combined into one statement


.exit
Each of the segments is called a logical segment. Depending on the memory the code and data
segment can be in the same of different physical segments according to the information given below:
Memory Models
Tiny
o Code & data combined must be less than 64K
Small
o One Code Segment & One Data Segment
o Each has size less than 64K
Medium
o One Data Segment of 64K &
o Multiple Code Segments
Compact
o One Code Segment of 64K &
o Multiple Data Segments
Large
o Code & Data Segments of > 64K &
o Multiple Code & Data Segments
Huge
o Both code and data can exceed 64k.
o Individual Variable can exceed 64k.
Stack Directive
Directive is .stack for stack segment. It should be declared even if the program itself doesn’t use
stack because it may be needed for temporary storage of variables and/or register content.

1
Memory Allocation
Directive is .data for data segment. All variables must be declared and memory space for each
allocated. Different data definition directives are used for different size types of memory:
a) DB => define byte (8 bits)
b) DW => define word (16 bits)
c) DD => define double word (32 bits)
d) DQ => define quadword (64 bits)
Code Segment
Directive is .code for code segment. The program resides here.
Example = > message db "Hello world", 13, 10, ‘$’
The string message contains ‘Hello world’ followed by Return (ASCII 13), Line-feed (ASCII 10)
and the ‘$’character. This method is very useful if we wish to include control characters (such as
Return) in a string. We terminate the string with the ‘$’ character because there is an MS-DOS
subprogram (number 9h) for displaying strings which expects the string to be terminated by the ‘$’
character.

Defining String Variables


The following 3 definitions are equivalent ways of defining a string "abc":
version1=> db "abc" ; string constant
version2=> db ‘a’, ‘b’, ‘c’ ; character constants
version3=> db 97, 98, 99 ; ASCII codes

Points to Remember:
-You need to start all assembly languages programs in a particular format (not necessarily that given
above.
-Your program must also finish in a particular format the end directive indicates where your program
finishes.
-In the middle comes the code that you write yourself.
-You must also specify where your program starts, i.e. which is the first instruction to be executed.
This is the purpose of the label, start. (Note: We could use any label, e.g. begin in place of start).
This same label is also used by the end directive.
-When a program has finished, we return to the operating system. Like carrying out an I/O operation,
this is also accomplished by using the int instruction. This time MS-DOS subprogram number 4c00h
is used.

What is the difference?


message db "Hello world", 13, 10, ‘$’
message db "Hello world", 13, 10, 36
message db "Hello world", 0dh, 0ah, 24h

Check the ASCII Table

2
DOS Display Functions/Interrupts
These are DOS functions 02 and 06 for single character display, and 09 for string display.

DOS functions 02 and 06


Both functions are identical, except that the function 02 can be interrupted by control break (Ctrl-
Break), while function 06 cannot. To display a single character ASCII character at the current cursor
position use the following sequence of instructions:

mov ah,06h ;or mov ah,02h


mov dl, character code
int 21h

The character code may be the ASCII code of the character or the character itself written between the
quotes.

DOS function 09
This function is used to display a string of characters ended with a ‘$’ sign.It’s syntax is as follows:
message DB “This is the message to be displayed”, “$”
.code
mov dx,offset message ;Copy address of message to dx
mov ah, 09h
int 21h

3
DOS Input functions/Interrupts
These include reading a single character, with or without echo, functions 01 and 08, and reading a
whole string.
DOS function 01 and 08
To read single character and have it echoed (displayed) on the screen, use the following code:
mov ah,01h
int 21h
;AL contains now the ASCII code of the character read from the keyboard

If the character is to be read without echo, such as reading a password, use the following code:
mov ah,08h
int 21h
;AL contains now the ASCII code of the character read from the keyboard

The following table summarizes the main I/O functions. These functions are mainly used to read and
display characters and/or strings read from the keyboard.

Examples:
1) Reading a character from the keyboard and displaying it on the screen:

4
Output: AA
Debug It:

Defining Constants: Macros


The equ directive is used to define constants. For example if we wish to use the names CR and LF,
to represent the ASCII codes of Carriage Return and Line-feed, we can use this directive to do so.
CR equ 13d
LF equ 10d

Simple Arithmetic Instructions


These instructions combine assignment with the arithmetic operation.
Example:
mov ax, 5 ; load 5 into ax
add ax, 3 ; add 3 to the contents of ax, ax now contains 8
inc ax ; add 1 to ax , ax now contains 9
dec ax ; subtract 1 from ax , ax now contains 8
sub ax, 6 ; subtract 6 from ax, ax now contains 2
The add instruction adds the source operand to the destination operand, leaving the result in the
destination operand. The destination operand is always the first operand in 8086 assembly
language.
The inc instruction takes one operand and adds 1 to it. It is provided because of the frequency of
adding 1 to an operand in programming. The dec instruction like inc takes one operand and subtracts
1 from it. This is also a frequent operation in programming. The sub instruction subtracts the source
operand from the destination operand leaving the result in the destination operand.
2) Assembly code to get uppercase letter from user and show the letter in lowercase:
Hint: ‘A’ - ‘a’ = 97 - 65 = 32

5
Task: Change Lower case to upper case & display

6
Home Tasks:
a)Write code to display your
NAME :
ROLLNO :
SEMESTER :

b) Write a code to print input char from user in the next line like the following:
A
A

c) Write a program that inputs two numbers, adds them and shows the result.
Limitation: The maximum sum can only be upto 9

Hint: 31h = 49d ASCII value for the char ‘1’


=‘1’ + ‘1’ = 31h + 31h Or 49 + 49 = 98 but it should be ASCII of 2 as 1+1=2
ASCII of 2 is 50d so we have to subtract some value from it :) To show correct result

Potrebbero piacerti anche