You are on page 1of 22

COLLEGE OF COMPUTING AND ENGINEERING (CCE)

CSP107 – Computer Organization and Assembly Language Programming

Laboratory Exercise No. 9 - 10

Segmentation and the Addressing Modes

Submitted by:

Andamon, Jerald
Evangelista, Renzo
Gutang, Cristian Jay
Tamesis, Zyra Mae
[Names]

3CS-A
[Section]

Submitted to:

Prof. Terrence A. Lim


[Name of Instructor/Professor]

GRADE

10/25/22
[Date]
Laboratory Exercise No. 9 - 10

Segmentation and the Addressing Modes

I. OBJECTIVES

At the end of the exercise, the students are expected to:

 Understand the physical and logical segmentation of the assembly


program;
 Determine the different addressing modes; and
 Learn how to calculate the physical and offset addresses.

II. EQUIPMENT/MATERIALS

The following equipment or materials will be needed to perform the laboratory


exercise:

 Emulator 8086
 Internet Connection for the submission
 USB for backup and file storage

III. PROCEDURE/DISCUSSION

Model Directives:
An assembly language program is written according to the following structure and includes the
following assembler directives:

TITLE “Optional: Write the Title of your program”

.MODEL SMALL - Assembler directive that defines the memory model to use in the
program. The memory model determines the size of the code, stack
and data segments of the program

.STACK - Assembler directive that reserves a memory space for program


instructions in the stack

.DATA - Assembler directive that reserves a memory space for constants and
variables

.CODE - Assembler directive that defines the program instructions

END - Assembler directive that finishes the assembler program


Each of the segments is called a logical segment. Depending on the memory, the code and data
segments may be in the same or in different physical segments according to Table 5.1.

Table 5.1: Memory Models

Memory
Model Size of Code and Data

TINY Code and Data no more than 64KB combined


SMALL Code and data segments must be no more than 64KB each
MEDIUM Code can be more than 64KB, data still limited to no more than 64KB
COMPACT Code limited to no more than 64KB, data can be more than 64KB
LARGE Code and Data can each be more than 64KB, no array can be larger
than 64KB
HUGE Code and data can each be more than 64KB, arrays can be larger
than 64KB

Stack Directive:

- Directive is .stack for stack segment


- Should be declared even if program itself doesn’t use stack needed for
subroutine calling (return address) and possibly passing parameters
- May be needed to temporarily save registers or variable content
- Will be needed for interrupt handling while program is running

Memory/Data Segment Directive:

- Directive is .data for data segment


- All variables must be declared
- Data definition directive can be followed by a single value, or a list of values
separated by commas
- Different data definition directives for different size types of memory:
-
1. DB – define byte (8 bits)
2. DW – define word (16 bits)
3. DD – define double word (32 bits)
4. DQ – define quad word (64 bits)

Code Segment Directive:

- Directive is a .code for code segment


- The “program” resides here

End of Program:

- Directive is end
- Tells assembler that this is the end of the program
Note:

The sequence of instructions at the beginning of a program used to assign the data
segment: MOV AX, @DATA
MOV DS, AX

May be replaced by the following directive .STARTUP which assigns both DATA and CODE
segments, and hence no warning will be issued by the assembler. However, it should be noted
that the program would start at address CS:0017h. The Startup directive occupies the bytes
CS:0000 to CS:0017. Identically, the sequence used to terminate and exit DOS can be replaced by
the .EXIT directive, which has exactly the same effect.

Data Representation

Numbers

• 11011 decimal
• 11011B binary
• 64223 decimal
• -21843D decimal
• 1,234 illegal, contains a non-digit character
• 1B4DH hexadecimal number
• 1B4D illegal hex number, does not end with “H”
• FFFFH illegal hex number, does not begin with a digit
• 0FFFFH hexadecimal number

• Signed numbers are represented using 2’s complement notation

Characters

• A character must be enclosed in single or double quotes: e.g. “Hello”, ‘Hello’, “A”, ‘B’
• The ASCII code is used to encode characters
• Examples:

• ‘A’ has ASCII code 41H


• ‘a’ has ASCII code 61H
• ‘0’ has ASCII code 30H
• Line feed has ASCII code 0AH
• Carriage Return has ASCII code 0DH
• Back Space has ASCII code 08H
• Horizontal tab has ASCII code 09H

Note: The value of a variable, the content of registers or memory is based on the programmer
interpretation:

• AL = FFH
•represents the unsigned number 255
•represents the signed number -1 (in 2’s complement)

• AH = 30H
•represents the decimal number 48
•represents the character ‘0’

• BL = 80H
•represents the unsigned number +128
•represents the signed number -128
Variable Declaration

 Each variable has a type


 Based on its definition, a variable is assigned a memory location
 The location is defined by its address and number of bytes
 Different data definition directives for different size types of memory
 DB define byte
 DW define word
 DD define double word (two consecutive words)
 DQ define quad word (four consecutive words)
 DT define ten bytes (five consecutive words)
 Each pseudo-op can be used to define one or more data items of given type

Byte Variables

The following directive defines a variable of size byte:


 Var_name DB initial value
 a question mark (?) place in initial value leaves variable non-initialized

Examples:
 I DB 4 define variable I with initial value 4
 J DB ? Define variable J with no initial value
 Name DB “Course” allocate 6 bytes for the variable Name
 K DB 5, 3, -1 allocates 3 bytes

Word Variables

The following directive defines a variable of size word:


 Var_Name DW initial value
 a question mark (?) place in initial value leaves variable non-initialized

Double Word Variables

The following directive defines a variable of size double word:


 Var_name DD initial value

Constant Definition

 The EQU pseudo-op is used to assign a name to a constant


 Syntax:
Cst_Name EQU Cst_Value
 No memory allocated for EQU names
 Makes assembly language easier to
understand
 Examples:

Example 1:

MOV DL, 0AH

Can be replaced by:

LF EQU
0AH
MOV DL,
LF
Example 2:

MSG DB “Type your name”

Can be replaced by:

PROMPT EQU “Type your


name” MSG DB PROMPT

Program Listing

Program 1

TITLE “Program 1”
; This program displays a string terminated by a $ sign using INT 21H function 09H.

.MODEL SMALL
.STACK 200
.DATA

MESSAGE1 DB 'This is the message to be displayed: ', '$'


MESSAGE2 DB 0DH, 0AH, 'The message you just entered: ', '$'
BUF DB 10 ; Number of characters to be read
DB 11 DUP('$') ; Reserve 10 bytes for string

.CODE

MOV AX, @DATA


MOV DS, AX

LEA DX, MESSAGE1


MOV AH, 09H
INT 21H

MOV AH, 0AH


MOV DX, OFFSET BUF
INT 21H

LEA DX, MESSAGE2


MOV AH,09H
INT 21H

LEA DX, BUF


ADD DX, 02
MOV AH, 09H
INT 21H

MOV AX, 4C00H


INT 21H

END

--------------------------------------------------------------------------------------------------------------------------------------
Program 2

TITLE "Program 2"


; This program displays a message and reads a new message from the keyboard

.MODEL SMALL
.STACK 200
.DATA

CRLFDB 0DH,0AH, '$'


PROMPT DB 'Enter a name max. length 30 char.: ', 0DH,0AH, '$'
BUFFER DB 31,32 DUP(?)

.CODE
.STARTUP ; this directive initializes the DS and CS segments.

LEA DX, PROMPT ; display prompt


MOV AH, 09H
INT 21H

MOV AH,0AH ; read into buffer


LEA DX, BUFFER
INT 21H

LEA DX, CRLF ; move cursor to next line


MOV AH, 09H
INT 21H

; now display the buffer i.e. what has been read.

MOV AH, 09H


MOV BH, 00H
MOV BL, BUFFER[1] ; move buffer in BL buffer length
MOV BUFFER[BX+2], '$' ; put a $ sign at the end of buf
LEA DX, BUFFER[2] ; load actual length of buffer
INT 21H

LEA DX, CRLF ; move cursor to next line


MOV AH, 09H
INT 21H

MOV AH, 02H ; display number of characters read if less than 10


MOV DL, BUFFER[1] ; read second byte of buffer
ADD DL, 30H ; convert to number
INT 21H

MOV AX, 4C00H


INT 21H

END
--------------------------------------------------------------------------------------------------------------------------------------
Program 3

TITLE "Program 3"


; This is a program that prompts the user to enter a lowercase letter, and on next line displays
; another message with letter in uppercase.

.MODEL SMALL
.STACK 200
.DATA

CR EQU 0DH
LF EQU 0AH
MSG1 DB 'Enter a lower case letter: $'
MSG2 DB CR, LF, 'In upper case it is: '
CHAR DB ?, '$'

.CODE
.STARTUP ; initialize data segment

LEA DX, MSG1 ; display first message


MOV AH, 9
INT 21H

MOV AH, 1 ; read character


INT 21H
SUB AL, 20H ; convert it to upper case

MOV CHAR, AL ; and store it


LEA DX, MSG2 ; display second message and
MOV AH, 9 ; uppercase letter
INT 21H

.EXIT ; return to DOS

END
--------------------------------------------------------------------------------------------------------------------------------------

PRE-LABORATORY WORK:

1. Compile and run the Program 1 (see program listing above), then save as
CSP107LE9-10A.asm.
2. Compile and run the Program 2 (see program listing above), then save as
CSP107LE9-10B.asm
3. Compile and run the Program 3 (see program listing above), then save as
CSP107LE9-10C.asm

LABORATORY WORK:

Write a program that prompts the user to enter a string, in capital letters, of a maximum length
of 20 characters. Read the string in capital letters and convert it to its lowercase. Then display the
new string and save the program as CSP107LE9-10.asm.
Note:

To convert a capital letter to a small one, use the following instruction:

; Read character
MOV AL, character_read
ADD AL, 20H
; Display character in AL register

Use the following to loop through the string you just entered.

MOV CX, Number_of_bytes_read


Again: Start loop here
; Convert to small
letters. LOOP Again

IV. DATA REPRESENTATION / OUTPUT PICTURES

Program 1

It
displays the first message

The message that we want to be displayed

The message that we entered


If the user entered Pamantasan,

the input that the user entered would not appear.

Save as CSP107LE9-10A.asm
Program 2

Length of the character – 10


Length of the character – 11

Length of the character – 12

Length of the
character – 13

Length of the
character –
14

Length of the character – 15


Length of the
character – 16

Length of the character – 17

Length of the
character – 30

Save as CSP107LE9-10B.asm
Program 3
It will display the first message

The lowercase letter that we entered

The lowercase letter that was converted into an uppercase letter


Save as CSP107LE9-10C.asm

LABORATORY WORK:
Save as CSP107LE9-10.asm
V. RESULTS INTERPRETATION/OBSERVATION

Program 1

In program 1, the program shows a string terminated by a $ sign by using the 21h
function 09h. According to the code, if the user enters the message that we want to be
displayed, the program will display the message that we entered. As you can see in the
picture, the input that we entered is PNC. After pressing the enter key, another display
message will appear, which is the input that we just entered. Based on our
observations, we discovered that if the user entered a character which is pamantasan,
the message that the user entered would not be displayed because the number of
characters to be read in the program has a maximum length of 9 characters.

Program 2

As you can see on the first picture, that what it’s look like when running the program.
The program will prompt a message where the user needs to enter a name max. length
30 characters. Based on the code, if the user enters a character that has a length that
less than 10. It will be converted into its equivalent in number and after that it will going
to be display. For example, on the second picture, user enter a character that has a
length of 5 and see what will going to be the output. Based on our observation we don’t
know if it is right, but we found out that if the user tries to enter a character that has a
length of 10 to 16 it will convert into a symbol and display it like on the next pictures.
Lastly, we observe that if the user tries to enter a character which has a length of 17 to
30 it will convert into a capital letter from A to N and display it. Example length of the
character is 17 & 30, if the user reaches the maximum length of the character, it will
display what name of character did the user enter, see the output on the next pictures.

Program 3

As shown in the fourth image in program 3, the program indicates that the 
user can enter a lowercase letter. According to the code, if the user can enter a
lowercase letter, it will be converted to an uppercase letter. As you can see in our
emulator screen, we use the letter z for lowercase letters. And in the following display
message, another message will be displayed in the program, which is the uppercase
letter that we entered in the lowercase letter. Based on our observations in the code,
we found out that when we enter a lowercase letter, it will be converted into an
uppercase letter.

Lab Work:
Based on my observation it appears when the software is running. The user will
receive a notification from the application asking for a top name which is 20 character
maximum. If the user type upper case it automatically converts into lower case, after
clicking enter. However, if the user types lowercase characters or strings with wrong
inputs it automatically loops in the program until the user was enter the uppercase.
After that, the program creates a string in the program. If you see your name in there it
will be written using the reverse order of your name, so the top part will be upper case,
then lower case, and finally the special characters or characters in the middle part.
However, there are other problems too.
VI. CONCLUSIONS

Program 1

In program 1, we learned how to display an input that has a maximum length of 9


characters. We also learned that the DUP directive commands the assembler to
duplicate an expression a given number of times. For example, in program 1 based on
the code, the number after DB indicates how many bytes to repeat for, and the '$'
indicates what to repeat for.

Program 2

This was the first time that we make and give an insight together in this group
laboratory. Some of us didn’t understand well the program but we tried to make others
understand. At first, we were all a bit confused as to how this program should work but
we did manage to observe it good like entering the name length by length. And after
many tries, we understand the output and that’s all.

Program 3

In program 3, we learned how to convert the lowercase letter into an uppercase


letter. We discovered that the standard DOS line-ending order is CR and LF. The
characters CR and LF are bytecode that can be used to indicate a line break in a
program. Our professor has already discussed CR and LF before, which are the
carriage return and line feed, respectively.

Lab Work:
I've concluded my conclusion that in order to successfully change the upper case to the
lower case, we need to read a character or string that has been entered into the
application, I need the code MOV AL to read the character or string in the program. In
order the event that the user types the incorrect input during the conversion, we require
the MOV CX, the number of bytes read in order to continue looping the program. To
avoid having the incorrect letter in the capital, we must find a means to change each
word to lowercase. We return the cursor since it should come before the return value
because the conversion was done inside of a loop. When the input line is finished or
the word quit is pressed, return values that do so. I must create and assign a number
between each byte in the text and the number between and within the line for this to
work on the first line of the program. I would advise only having a 1-6 digit number
between each program byte and the 1-6 digit within the line when these parameters
are set to lowercase. We can set the current state of the word character by altering the
current state of the input character when changing the beginning character name from
"L" to "B." I must use the set of settings from above to modify this system in any way.
VII. STUDENT OUTCOMES ADDRESSED

(… to fill out by your instructor)

VIII. APPENDICES

A. RUBRICS AND SCORING

(… kindly refer to rubrics and scoring provided)

You might also like