Sei sulla pagina 1di 10

Activity No.

3
DATA DEFINITION AND TRANSFER
Course Code: Cp413L1
Program:BSECE
Course Title: Computer Systems Organization with Assembly Language Date Performed:08/10/15
Section:EC31FA2
Date Submitted:08/11/15
Name: Ritz Kenneth J. Eledia
Instructor:Engr.Vicente
1. Objective:
This activity aims to demonstrate how characters and string of characters move from one location
to another.
2. Intended Learning Outcomes (ILOs):
After completion of this activity the students should be able to:
2.1 Show how to define data in Assembly
2.2 Declare a group of characters
2.3 Write programs applying Service 09 , Int 21
3. Discussion :
Data Definition
The basic storage unit for data in a computer is a byte, containing 8 bits(1 byte), other storage
sizes are word (2 bytes), doubleword (4 bytes) , and quadword (8 bytes).
String is stored in memory as a succession of bytes containing ASCII codes. They must be defined
in the computer through directives. Table 3-1 shows how they are defined using directives.
Term
Byte
Word
Doubleword
Quadword

Table 3-1-Data Directives


Number of bits
Directive
8
DB (Define Byte)
16
DW (Define Word)
32
DD (Define Double Word)
64
DQ ( Define Quadword)

DISPLAYING A STRING
There are two ways to display a string:
Using Service 09H
Required:
1 The string must be defined in DATA segment.
2

The string must be terminated by '$'.

AH = 09h

DX = Offset address of the beginning of the string

Example:
.DATA
STRING_NAME DB 'THE STRING TO BE DISPLAYED$'
.CODE

MOV AH , 09H
MOV DX , OFFSET STRING_NAME
INT 21H
Note:

If the terminating $ is omitted after the string, the operation displays characters in the
memory, until it finds a $ character, if any.
To move the cursor to the beginning of the next output line, put 0Dh and 0Ah after the string
and before the terminating $.

Example:
PROMPT DB 'PLEASE, ENTER YOUR NAME: ' , 0Dh , 0Ah , '$'
Another way of moving the cursor to the beginning of the next output line is to display , using DOS
function 09H, a string of the form:
STRING1 DB 0Dh , 0Ah , '$'
Using Service 40H
Required:
1
2
3
4

Set AH = 40h
BX = 1
CX = string length
DX = offset address of the beginning of the string

Example:
.DATA
STRING_NAME DB 'THE STRING TO BE DISPLAYED'
STRINGLEN EQU $ STRING_NAME
.CODE

MOV AH , 40H
MOV BX , 01H
MOV CX , STRINGLEN
; string length
MOV DX , OFFSET STRING_NAME
INT 21H
The EQU directive defines a value that the assembler can use to substitute in other instructions.
An operand containing a dollar symbol, $, refers to the current value in the location counter. Thus,
in the above example $ - STRING_NAME evaluates to the number of bytes between
STRING_NAME and STRINGLEN which is the number of bytes (i.e., characters) in THE STRING
TO BE DISPLAYED

READING A STRING (SERVICE 0Ah)


To read a string, we have to do two steps:
a
b

Define an array to store that string in DATA segment.


Invoke DOS function 0AH in CODE segment.

One way of defining the array is:


BUFFER_NAME DB Num1 , Num2 DUP(?)

where:
a
b
c
d

Num1 = the maximum number of string characters to be read + 1


Num2 = Num1 + 1
The maximum value for Num1 is FFh i.e., 255
The last string character is the Carriage Return (0Dh).

The program will wait for the input. The user must press "Enter" key to end the inputting process. inp
Example:
Define an array called STRING to store a string of maximum length 50 characters to be used by the service
0Ah of INT 21h.
Actual string length is stored in this byte
To read a string from the keyboard into an array called BUFFER as defined above, we invoke DOS function
0AH as:
MOV AH , 0AH
MOV DX , OFFSET STRING
INT 21H
The operation echoes the entered characters on the screen and advances the cursor.
To display the above array generally by using DOS function 40H:
MOV AH, 40H
MOV BX, 01H

; file handle for the screen

MOVZX CX, STRING[1]

; Initialize CX with the string length

MOV DX, OFFSET STRING[2]


INT 21H
4. Resources:
Computer with 32-bit Operating System
TASM
5. Procedure:
Sample Program A.
1 Type the following program using Notepad.
dosseg
.model small

.stack
.data
prompt1 db 13,10,"Enter a character:$"
prompt2 db 13,10,"The character you entered is:$"
.code
main proc
mov ax,@data
mov ds,ax
lea dx,prompt1
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bl,al
lea dx,prompt2
mov ah,09h
int 21h
mov dl,bl
mov ah,02h
int 21h
mov ax,4c00h
int 21h
main endp
end
2
3
4
5

Save the file inside the TASM directory as progA.asm.


Open the DOS command prompt.
Change directory to TASM. Type,
C:\> cd TASM <Enter>
Assemble Proga.asm. Type,
C:\tasm> tasm progA.asm<Enter>
The following message will appear if you assembled your program successfully.
Turbo Assembler Version 2.0 Copyright (c) 1988, 1990 Borland International
Assembling file: proga.ASM
Error messages: None
Warning messages: None
Passes:
1
Remaining memory: 442k
Use tlink to link all files created from assembling. Type,
C:\tasm> tlink ProgA.obj<Enter>
Turbo Link Version 3.0 Copyright (c) 1987, 1990 Borland International

Execute the program created. Type,


C:\tasm> ProgA<Enter>
8 Analyze and record the output in Table 3.2.
Sample Program B.
Type the following program using Notepad.
.model small
.stack 100h
.data
byte1 db 1
byte2 db 0
word1 dw 1234h
word2 dw 0
string db "Stressed!", 0dh, 0ah,"$"
;---- this is a comment
.code
MAIN PROC
Mov ax, @data
Mov ds, ax
Mov dx, offset string
Mov ah, 9
Int 21h
Mov bx, offset string
Mov al, [bx]
Mov ah, [bx+1]
Mov [bx], ah
Mov [bx+1],al
Mov ah,9
Mov dx, offset string
Int 21h
Mov ax, 4c00h
Int 21h
Main endp
End main
Save the program as ProgB.asm.
Assemble, link and execute the program.
Analyze and record the output in Table 3.3
6. DATA ANALYSIS:

Table 3.2- Output of Sample Program A

Table 3.3- Output of Sample Program B

PROBLEMS:
1 How many bytes are allocated for each of the following data definitions?
a BYTE 20 DUP(0)
= 20 bytes
b BYTE 20 DUP (?)
= 20 bytes
c BYTE 4 DUP(East) = 16bytes
d WORD3 WORD ?
= 16 bytes
e Array WORD 5 DUP(?) = 80 bytes
2 Write a program that prompts and reads a users name USERNAME (of maximum length 30
characters). The program should display a message of the form:
OUTPUT:
Hello, What's your name? JM
Hello, JM
Congratulations! Your first program is working!
Dosseg
.model small
.stack 100h
.data

A db 13,10,Hello, whats your name?$


B db Hello $
C db 13,10,Congratulations! Your first program is working!$
D db 24 dup($)
E db 13,10,$
.code
Start:
Mov ax, @data
Mov ds,ax
Mov es, ax
Mov ah, 00h
Int 10h
Mov dx,offset A
Mov ah, 09h
Int 21h
Mov byte ptr D,30
Mov dx, offset D
Mov ah, 0Ah
Int 21h
Mov dx, offset E
Mov ah, 09h
Int 21h
Mov dx, offset B
Mov ah, 09h
Int 21h
Mov si, 002
Lea dx, D [SI]
Mov ah, 09h
Int ,21h
Mov dx,offset C
Mov ah, 09h
Int 21h
Mov ah, 4ch
Int 21h
End start
3

Modify ProgB such that the second string is printed !dessertS (Stressed! backwards).
.model small
.stack 100h

.data
byte 1 db 1
byte 2 db 0
word1 dw 1234h
word2 dw 0
S db "Stressed!",0dh,0ah,"$"
;---- this is a comment
.code
main proc
mov ax, @data
mov ds, ax
mov dx, offset S
mov ah, 9
int 21h
mov bx, offset S
mov al, [bx]
mov ah, [bx+8]
mov [bx],ah
mov [bx+8], al
mov al,[bx+1]
mov ah,[bx+7]
mov [bx+1],ah
mov [bx+7], al
mov al,[bx+2]
mov ah,[bx+6]
mov [bx+2], ah
mov [bx+6], al
mov [bx+3], ah
mov [bx+5], al
mov ah, 9
mov dx, offset S
int 21h
mov ax, 4c00h
int 21h
main endp
end main
4

Create a program that will display a given string two times.


SAMPLE OUTPUT:
Enter a String: love
lovelove
CODE:
dosseg
.model small
.stack 100h

.data
X db 13,10, "Enter a String:$"
Y db 24 dup("$")
Z db 13, 10,"$"
.cod
main proc
mov ax, @data
mov ds, ax
mov es,ax
mov al,03h
mov ah,00h
int 10h
mov dx, offset X
mov ah, 09h
int 21h
mov byte ptr Y, 30
mov dx, offset Y
mov ah, 0Ah
int 21h
mov dx, offset Z
mov ah, 09h
int 21h
mov si, 02h
lea dx, Y[SI]
mov ah, 09h
int 21h
mov bl, Y[1]
mov dx, offset[Y+2]
mov ah, 09h
int 21h
mov al, bl
mov dh,2h
mov dl, al
mov bh,0
mov ah, 2h
int 10
mov si, 02h
lea dx, Y[SI]
mov ah, 09h
int 21h
mov ax, 4c00h
int 21h
man endp
end

8. Assessment (Rubric for Laboratory Performance):

Potrebbero piacerti anche