Assembly Language
Assembly Language
1
Debug
Course Code: CPE 005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate how Debug can be used as an assembler and disassembler.
To create a program in assembly options are to use an assembler or a debugger. The debugger that
comes with both Windows and DOS is debug.exe or DEBUG program.
Debug commands are issued at command prompt, denoted by a hyphen (-), either as a single
letter, in uppercase or lowercase, or with a combination of parameter. Table 1.1 shows the summary of the
Debug commands. The “?” is used to show the long name of each command.
Command Description
Program Creation and Debugging
A Assemble a program using instruction mnemonics
G Execute the program currently in memory
R Display the contents of register
P Proceed past an instruction, procedure or loop
T Trace a single instruction
U Disassemble memory into assembler mnemonics
Memory Manipulation
C Compare one memory range with another
D Dump (display) the contents of memory
E Enter bytes into memory
F Fill a memory range with a single value
1
M Move bytes from one memory range to another
S Search a memory range for specific values
Miscellaneous
H Perform Hexadecimal addition and subtraction
Q Quit Debug and return to DOS
Input-Output
I Input a byte from a port
L Load data from disk
O Send a byte to a port
N Create a filename for use by the L and W commands
W Write data from memory to disk
Once a Debug command has been issued, the <Enter> key must be pressed so that the command
becomes effective.
4. Resources:
Computer with 32-bit Operating System
Debug.exe
5. Procedure:
Sample Problem A.
1. Open Debug.exe. Type the following at the DOS command prompt of your computer:
C:/>Debug <Enter>
2. Issue the command E when prompted by hyphen ( - ), to place bytes in the memory,
- E CS:100 “Welcome to Assembly Programming!”
3. Use the D command to view contents at CS:0100
-D 100
Describe how your data are stored in the memory of the computer. In what number system are
they stored?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Record all results in Table 1.2.
4. Display the contents of a range, type
-D 110 11F
Describe what happened to the display.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
5. Type Q to quit from Debug.
-Q
2
Sample Problem B.
1. Open Debug.exe.
2. Assemble the following program at address CS:0100. Type,
-A 100
138C:0100 mov ah,02
138C:0102 mov dl,49
138C:0104 int 21
138C:0106 mov dl,20
138C:0108 int 21
138C:010A mov dl,03
138C:010C int 21
138C:010E mov dl,20
138C:0110 int 21
138C:0112 mov dl,54
138C:0114 int 21
138C:0116 mov dl,49
138C:0118 int 21
138C:011A mov dl,50
138C:011C int 21
138C:011E int 20
138C:0120 [Enter]
-
3. Execute the program, type Gat the debug prompt.
-G
4. Analyze the output and record all results in Table 1.3
5. Unassemble the program at address 100 to 11e, type
- U 100 11E
6. Observe the output and record all results in Table 1.4.
7. Edit the values of address 107 and 10f, type
-E CS:107
20.0A
-E CS:10F
20.0A
8. Unassemblethe program at address 100
-U 100
Observe what happened to the output and record all results in Table 1.5.
What changes have you observed in the values of the memory?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
9. Execute the program. Type,
-G
10. Save the program as Prog1.com, type
-N c:\CompOrg\Prog1.com <Enter>
Note: You should have had created CompOrg directory in C: drive.
3
11. Use H command to get the sum and difference of the addresses 0120 and 0100. Type,
–H 0120 0100
0220 0020
12. Change the value of the CX register to 0020. Type,
-R CX <Enter>
CX 0000
:0020
13. Type W to write the file Prog1.com,
-W <Enter>
The computer should display “Writing 00020” bytes.
14. Exit from Debug.
Sample Problem C.
1. Check the created Prog1.com file. Open the DOS command prompt. Click START-type CMD-press
ENTER.
2. Change directory to CompOrg.
c:\cd CompOrg><Enter>
3. Execute Prog1.com.
c:\CompOrg>Prog1.com <Enter>
4. Record your output in Table 1.6.
Sample Problem D.
1. Open Debug.exe
2. Type ?
-?
Describe what happened after you typed “?” at the prompt.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Use Table 1.7 to write the long names of the commands and given the examples,give the function
of each of the commands used in this activity.
4
6. DATA ANALYSIS:
138C:0100 ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ Welcome to Assem
138C:0110 ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ bly Programming!
138C:0120 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
138C:0130 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
138C:0140 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
138C:0150 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
138C:0160 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
138C:0170 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
--
5
Table 1.5-Result of Sample Problem B.6
6
Table 1.7- Debug Commands
Q Q
W W
7
7. PROBLEMS:
1. Create a DEBUG program that will display your name, program, and course, on the computer
screen. Save you program as Prog2.com.
Sample Output:
Juan dela Cruz
BS CpE
CPE005
8. CONCLUSIONS
8
9. Assessment (Rubric for Laboratory Performance):
9
Activity No. 2
Registers
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate how the CPU registers are used for addition, subtraction,
multiplication and division operations and how the flag register are affected by these operations
The 8088 CPU for example, has fourteen 16 bit registers to process the data in a computer.
The general-purpose registers are primarily used for arithmetic and data movement. Each register can
be addressed as either a single 32-bit value or two 16-bit values. Portions of some registers can be
10
addressed as 8-bit values.
For example, the 32-bit EAX register has a 16-bit lower half named AX. The AX register, in turn, has
an 8-bit upper half named AH (A-High) and an 8-bit lower half named AL(A-Low).
The same overlapping relationship exists for the EAX, EBX, ECX, and EDX registers.
The remaining general-purpose registers only have specific names for their lower 16 bits, these are
used when writing real-address mode programs.
THE FLAGS
The EFLAGS (or just Flags) register consists of individual binary bits that control the operation of the
CPU or reflect the result of arithmetic and logical instructions. Some instructions test and manipulate
individual processor flags.
A flag is set when it equals 1; it is clear (or reset) when it equals 0. Figure 2-1 shows how each flag is
represented by DEBUG.
11
The DIV command is used for division.
In multiplication and division operations, the x86 microprocessor use the registers AX, AL, AH, EAX, DX
and EDX as used as shown in Table 2.1 and Table 2.2
Table 2.2- Default Operands for Multiplication
No. of Bits Multiplicand Multiplier Product
8 bits x 8 AL register or memory AX (16 bits)
16 bits x 16 AX register or memory DX:AX (32 bits)
32 bits x 32 EAX register or memory EDX:EAX (64 bits)
The operands can be considered as signed numbers or unsigned numbers. The unsigned multiplication
and division operations are MUL, DIV.
12
What happened to the value of AX register?
______________________________________________________________________________
5. Change the value of the Parity flag, from Parity Odd (default) to Parity Even. Type
-R F
NV UP EI PL NZ NA PO NC -PE<Enter>
6. Check if the value of the Parity flag changed. Type,
-R F<Enter>
What is now the new value in of the flag register?
______________________________________________________________________________
7. Reset the values of the registers. Type,
-Q
Sample Problem B:
1. Open Debug.exe
2. Assemble the following program:
-A 100
movax,ffff
add ax,01
int 21
3. Trace the values of the registers starting at address 0100
-T=100 2
4. Observe the output.
What did you observe in the output? Why?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
5. Record all results in Table 2.5.
6. Reset the values of the registers.
Sample Problem C.
1. Open Debug.exe.
2. Assemble the following program:
-A 100
mov al,00
sub al,01
int 21
2. Trace the values of the registers.
3. Observe the output.
Which flag/flags was/were affected by the given? Why?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
4. Record all results in Table 2.6.
5. Reset the values of the registers.
13
Sample Problem D.
1. Open Debug.exe.
2. Assemble the following program:
-A 100
mov al,0a
mov bl,05
mulbl
int 21
9. Trace the values of the registers.
10. Observe the output.
What did you observe in the output? Why?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
11. Record all results in Table 2.7.
12. Reset the values of the registers.
Sample Problem E.
1. Open Debug.exe.
2. Assemble the following program:
-A 100
mov dx,0
mov ax,8003
mov cx,100
div cx
int 21
3. Trace the values of the registers.
4. Observe the output.
What did you observe in the output? Why?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
5. Record all results in Table 2.8.
6. Reset the values of the registers.
14
6. DATA ANALYSIS:
Table 2.4- Result of Sample Problem A.2
AX BX CX DX IP
MOV AX,FFFF
ADD AX,01
INT 21h
AX BX CX DX IP
MOV AL,00
SUB AL,01
INT 21h
15
Table 2.5 -Result of Sample Problem C.5
Instruction Register Content Flag Register
AX BX CX DX IP
MOV AL,0a
MOV BL,05
MUL BL
INT 21h
AX BX CX DX IP
MOV DX,00
MOV AX,8003
DIV CX
INT 21h
16
7. PROBLEMS:
1. Determine two 8-bit numbers that will cause the following flag conditions to occur after the addition.
Verify that your numbers cause thespecified flag conditions by modifying your program with your
new numbers, executing it, andrecording the flag values. Use HEX numbers.
a. Carry = 0, Overflow = 0, Zero = 0, Sign = 0: ___________ + ___________ = ___________
b. Carry = 0, Overflow = 0, Zero = 0, Sign = 1: ___________ + ___________ = ___________
c. Carry = 1, Overflow = 1, Zero = 0, Sign = 0: ___________ + ___________ = ___________
d. Carry = 0, Overflow = 1, Zero = 0, Sign = 1: ___________ + ___________ = ___________
e. Carry = 1, Overflow = 0, Zero = 0, Sign = 1 : ___________ + ___________ = ___________
f. Carry = 1, Overflow = 0, Zero = 0, Sign = 0 : ___________ + ___________ = ___________
g. Carry = 0, Overflow = 0, Zero = 1, Sign = 0 : ___________ + ___________ = ___________
8. CONCLUSIONS
17
9. Assessment (Rubric for Laboratory Performance):
18
Activity No. 3
DATA DEFINITION AND TRANSFER
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate how characters and string of charactersmove from one location to
another.
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 '$'.
3. AH = 09h
4. 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
19
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. Set AH = 40h
2. BX = 1
3. CX = string length
4. 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’
20
where:
a. Num1 = the maximum number of string characters to be read + 1
b. Num2 = Num1 + 1
c. The maximum value for Num1 is FFh i.e., 255
d. 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
movax,@data
movds,ax
lea dx,prompt1
mov ah,09h
int 21h
mov ah,01h
21
int 21h
movbl,al
lea dx,prompt2
mov ah,09h
int 21h
movdl,bl
mov ah,02h
int 21h
mov ax,4c00h
int 21h
main endp
end
22
MAIN PROC
Mov ax, @data
Mov ds, ax
6. DATA ANALYSIS:
Table 3.2- Output of Sample Program A Table 3.3- Output of Sample Program B
7. PROBLEMS:
1. How many bytes are allocated for each of the following data definitions?
a. BYTE 20 DUP(0)= _____________ bytes
b. BYTE 20 DUP (?)= _____________ bytes
c. BYTE 4 DUP(“East”)= _____________ bytes
d. WORD3 WORD ?= _____________ bytes
e. Array WORD 5 DUP(?)= _____________ bytes
2. Create a program that prompts and reads a user’s name USERNAME (of maximum length 30
characters). The program should display a message of the form:
23
OUTPUT:
Hello, What's your name? JM
Hello, JM
Congratulations! Your first program is working!
3. Modify ProgB such that the second string is printed “!dessertS” (“Stressed!” backwards).
8. CONCLUSIONS
24
9. Assessment (Rubric for Laboratory Performance):
25
Activity No. 4
C TRANSLATION TO ASSEMBLY LANGUAGE
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to show the relationship of C programming language to assembly language
CMP_ instruction
Conditional_loop instruction <label>
Instruction1
Instruction2
:
Instruction
JMP_instruction<label>
26
The WHILE statement in C language of the form:
do
{
Statement1;
Statement2;
:
statementn
} while (expression);
Can be implemented in Assembly language as:
label:
Instruction1
Instruction2:
Instruction
CMP instruction
Conditional_jump instruction
Label
The DO WHILE statement in C language of the form:
While
{
Statement1;
Statement2;
:
statementn
}
Can be implemented in Assembly language as:
Label1:
CMP instruction
Conditional jmp instruction label
Instruction1:
Instruction2
:
instructionn
JMP label
Conditional_jump instruction
Label2:
Instruction1:
Instruction2
:
Instruction
Label
Acts as an identifier that acts as a place marker for instructions and data. When placed just before
an instruction implies the instruction’s address. If placed just before a variable implies the variable’s
address.
Loops
Loops or repetition allow a set of instructions to be repeated until certain condition is reached is also
used in Assembly using the LOOP command.
27
4. Resources:
Computer with 32-bit Operating System
TASM
5. Procedure:
Sample Problem 1:
1. Type the following programs in Notepad.
TITLE prog4_1.asm TITLE prog4_2.asm
Dosseg .model small
.model small .stack
.stack 0100h .data
.data .code
.code movax,@data
movax,@data mov ds, ax
mov ds, ax mov cx,001Eh
mov cx,001Eh mov ah,02h ;request display character
mov ah,02h ;request display character movdl,'A' ;character to display
mov dl,'*' ;character to display B: int 21h ;call interrupt service
A: int 21h ;call interrupt service inc dl
loop A loop B
mov ax, 4c00h ;end mov ax, 4c00h ;end
int 21h int 21h
end end
2. Assemble and execute these programs.
3. Analyze the outputs.
What did you observe about the outputs?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
4. Record the outputs in Table 4.1 and Table 4.2 respectively.
Sample Problem 2:
1. Type the following programs in Notepad.
TITLE Equal.asm // Equal.c
MAIN SEGMENT #include<stdio.h>
ASSUME #include<conio.h>
CS:MAIN,DS:MAIN,ES:MAIN,SS:MAIN main()
ORG 100h {
START: int DH,DL;
MOV DL,41h DL = 41;
MOV DH,41h DH = 41;
CMP DH,DL if (DH == DL)
JE TheyAreEqual printf("Y");
JMP TheyAreNotEqual else
TheyAreNotEqual: printf("N");
MOV AH,02h getch();
MOV DL,4Eh return 0;
28
INT 21h }
INT 20h
TheyAreEqual:
MOV AH,02h
MOV DL,59h
INT 21h
INT 20h
MAIN ENDS
END START
TITLE Triangle //Triangle.c
.model small #include<stdio.h>
.code #include<conio.h>
org 100h main()
start: {
int z=1;int x=0;int y=4;
mov cl,1
mov bl,0
mov ch,4
while (y>0)
looprow:cmp ch,0 {
jgloopcol
jmp quit
while(x<z)
loopcol: {
cmpbl,cl
jldsplay
jmp next
printf("*");
dsplay:mov ah,2h x++;
mov dl,'*' ;display asterisk }
int 21h
incbl
jmploopcol
printf("\n");;
next:mov dl,0Ah x=0;y--;z++;
int 21h ;next line }
mov dl,0Dh
int 21h
mov bl,0
decch
inc cl
jmplooprow
quit:int 20h getch();
end start return 0;}
2. Assemble and execute each program.
29
3. Observe the output.
What did you observe about the output?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
4. Record the output in Table 4.3 and Table 4.4
6. DATA ANALYSIS:
Table 4.3 Output of Program Equal Table 4.4 Output of Program Triangle
7. PROBLEMS:
1. Translate the following C program to their equivalent assembly codes. Use the space provided.
//Prog4_1.c ;;
#include<stdio.h>
#include<conio.h>
main()
{
int cx;
for (cx=0;cx<5; cx++)
printf("*");
getch();
return 0;
}
30
//Prog4_2.c ;
#include<stdio.h>
#include<conio.h>
main()
{
void print();
print();
getch();
return 0;
}
void print()
{
int cx=1;
while (cx<=5){
printf("*");
cx++;}
}
//Prog4_3.c ;
#include<stdio.h>
#include<conio.h>
main()
{
char message[]="Hello World!";
printf("%s",message);
getch();
return 0;
}
31
2. Convert the each of the following C codes into its equivalent assembly code:
a. if ( ebx<=ecx) { eax=5;edx=6;}
3. Show a program that multiples 50 (decimal) and 10 (decimal) without using the MUL and IMUL
instructions.
8. CONCLUSIONS:
32
9. Assessment (Rubric for Laboratory Performance):
33
Activity No. 5
LOGICAL INSTRUCTIONS
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate the logical instructions AND, OR XOR, and NOT.
Boolean Expression
A boolean expression involves a boolean operator and one or more operands. Each boolean expression
implies a value of true or false. The set of operators includes: AND,OR, XOR and NOT.
The AND
AND syntax:
AND REGISTER1, REGISTER2
AND REGISTER, VALUE
AND returns 1 (TRUE) only if BOTH operands are 1 (TRUE)
The AND operation is useful for CLEARING particular bits in an operand ('0' AND anything ='0'). For
example, the operation: AND AL, 0Fh will set bits B7-B4 to '0', and leave bits B3-B0 unaffected.
The OR
OR syntax:
OR REGISTER1, REGISTER2
OR REGISTER, VALUE
OR returns 1 (TRUE) if either operand is 1 (TRUE)
The OR operation is useful for SETTING particular bits in an operand ('1' OR anything = '1'). For example,
the operation: OR AL, 0Fh will set bits B3-B0 to '1', and leave bits B7-B4 unaffected.
The XOR
XOR syntax:
XOR REGISTER1, REGISTER2
XOR REGISTER, VALUE
XOR returns 1 (TRUE) if one or the other operand is 1 (TRUE), but not both
34
The XOR operation is useful complementing bits in an operand ('1' XOR anything =not(anything)). For
example, the operand XOR AL, 0Fh will complement bits B3-B0 and leavebits B7-B4 unaffected. The XOR
operation can also be used to clear a register to zero – theoperation XOR AX, AX will set AX to zero (this
requires less machine code than MOVAX,0000).
The NOT
NOT syntax:
NOT REGISTER
NOT VALUE
The NOT reverses or complements the value it follows.
4. Resources:
Computer with 32-bit Operating System
TASM
5. Procedure:
Sample Problem 1:
1. Type the following program in Notepad.
ogic.asm
TITLE logic.asm
.model small
.stack 100h
.data
myStringdb "Proud to be TIPians","$"
.code
main proc
movax,@data
movds,ax
movbx,offsetmyString
LP1: mov dl,[bx]
Cmp dl, '$'
Je exit
Incbx
;insert code here
mov ah,02
int 21h
jmp lp1
Exit: Mov ax, 4c00h
Int 21h
Main endp
End main
2. Save the program as logic.asm
3. Assemble and execute the program.
4. Analyze the output and record the output in Table 5.1
35
Sample Problem 2:
1. Modify program logic.asm.
2. Replace the line “; insert code here”, with "and dl, 11011111B" .
3. Save the program as and.asm.
4. Assemble and execute the program.
5. Observe and record the output in Table 5.2
6. How is your output different from before? Why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Sample Problem 3:
1. Modify logic.asm again, this time replace the line “;insert code here”, with “xor dl, 00100000B".
2. Save the program as xor.asm.
3. Assemble and execute the program.
4. Observe and record the output in Table 5.2.
How is your output different from before? Why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Sample Problem 4:
1. Modify logic.asm once again, this timeplace the line “;insert code here”, with "or dl, 00100000B".
2. Save the program as or.asm.
3. Assemble and execute the program.
4. Observe and record the output in Table 5.3.
5. How is your output different from before? Why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
6. DATA ANALYSIS:
36
7. PROBLEMS:
1. Write an assembly program that will simulate the given Boolean expression using assembly
programming.
AL = (AH∙ BH + AL∙ BL)’ xor (CL+(CH∙ DH)’ ∙ DL)’
8. CONCLUSIONS:
37
9. Assessment (Rubric for Laboratory Performance):
38
Activity No. 6
BIT MANIPULATION
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate bit manipulation in Assembly Language.
Bit Manipulation
Bit manipulation allows for shifting or rotating bits in an operand. It is one of the advantages of
assembly language over high level languages. The computer can perform arithmetic and logical shifts.
Arithmetic shift fills the newly created bit position with a copy of the number’s sign bit.Logical Shift fills the
newly created bit position with zero.
The SHL (shift left) instruction performs a logical shift on the destination operand, filling the lowest
bit with 0. When a bit is shifted to the left once, the number is multiplied by 2. For example if we have MOV
DL,6 SHL DL,1 the value 0000 0110 becomes 00001100, therefore SHL is used as fast multiplication
instruction.
SHR (Shift Right) instruction performs a logical right shift on the destination operand. The highest
bit position is filled with a zero.
SAL (Shift Arithmetic Left) is identical to SHL
SAR (Shift Arithmetic Right) performs a right arithmetic shift on the destination operand.
ROL (Rotate) shifts each bit to the left. The highest bit is copied into both the Carry flag and into
the lowest.
ROR (Rotate Right) shifts each bit to the right, the lowest bit is copied into both the carry flag and
into the highest bit. No bits are lost in the ROR operation.
RCL (Rotate Carry Left) shifts each bit to the left. It copies the flag to the least significant bit and
the most significant bit to the carry flag.
RCR (Rotate Carry Right) shifts each bit to the right. It copies the most significant bit to the flag and
least significant bit to the flag.
4. Resources:
Computer with 32-bit Operating System
TASM
39
5. Procedure:
Sample Problem A.
1. Type the following program in a Notepad.
TITLE bit.asm
.model small
.stack 100h
.data
num db 03Dh
.code
main proc
movax,@data
movds,ax
movbl,num
mov cx,8
here: shr bl,1
Jcis_one
Mov dl,30h
Jmp print
Is_one:
Mov dl,31h
Print:
Mov ah,2
int 21h
loop here
Exit: Mov ax, 4c00h
Int 21h
Main endp
End main
2. Save the program as bit.asm.
3. Assemble and execute the program.
4. Analyze and record the output in Table 6.1.
Sample Problem B.
1. Modify program bit.asm, replace line number 5 with “ num db 0ah “.
2. Save the program as bit1.asm.
3. Assemble and execute the program.
4. Analyze and record the output in Table 6.2.
Did you get the expected output? Why or Why not?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
40
Sample Problem D.
1. Modify program bit.asm, replace line number 12 with “here: shr bl,1”.
2. Save the program as bit2.asm.
3. Assemble and execute the program.
4. Analyze and record the output in Table 6.2.
Did you get the expected output? Why or Why not?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Sample Problem C.
1. Modify program bit.asm, replace line number 12 with “here: rcr bl,1”.
2. Save the program as bit3.asm.
3. Assemble and execute the program.
4. Analyze and record the output in Table 6.2.
Did you get the expected output? Why or Why not?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
6. DATA ANALYSIS:
7. PROBLEMS:
1. Modify bit.asm so that it uses 64-bit value stored at NUM. The 64-bit number should be printed
from the most significant bit to the least significant bit.
2. Write an assembly program that will clear the most significant nibble, set the least significant nibble
and retain the values of the rest of the bits of AX.
3. Make a program that will implement the following 10*ax = 8*ax + 2*ax using shift or rotate
instructions.
41
8. CONCLUSIONS:
42
43
Activity No.7
PROCEDURE
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Language Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate how stack works in Assembly Language
44
Calling a Procedure has the form:
CALL <procedure name>
Returning from a Procedure has the form:
BODY proc
call <procedure1>
call <procedure2>
BODY endp
PROCEDURE1 proc
:
ret
PROCEDURE1endp
PROCEDURE2 proc
:
ret
PROCEDURE2endp
4. Resources:
Computer with 32-bit Operating System
TASM
5. Procedure:
Sample Program A.
1. Type the following in Notepad.
TITLE proc1.asm
.model small
.stack 100h
.data
.code
main proc
movax,@data
movds,ax
xoral,al
mov cx,16
lp1: push ax
call out1hex
call pcrlf
pop ax
inc al
loop lp1
Mov ax, 4c00h
Int 21h
Main endp
Out1hex proc
And al,0fh
Cmp al,9
45
Ja ischar
Add al,30h
Jmpprintit
Ischar: add al,37h
Printit: Movdl,al
Mov ah,2
Int 21h
Ret
Out1hex endp
Pcrlfproc
Mov dl,0ah
Mov ah,2
Int 21h
Mov dl,0dh
Mov ah,2
Int 21h
Ret
Pcrlfendp
End main
2. Save the program as proc1.asm.
3. Assemble and execute the program.
4. Analyze the output and record the output in Table 7.1.
What does the procedure Pcrlfdo?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Sample Program B.
1. Modify proc1.asm, interchange the placement of procedure Out1hex with Pcrlf.
2. Save the program as proc2.asm.
3. Assemble and execute the program.
4. Analyze the output and record the output in Table 7.2.
How is your output different from before? Why?
___________________________________________________________________________
___________________________________________________________________________
______________________________________________________________________________
Sample Program C.
1. Modify proc1.asm, change line number 15 with “inc al”.
2. Save the program as proc3.asm.
3. Assemble and execute the program.
4. Analyze the output and record the output in Table 7.3.
How is your output different from before? Why?
___________________________________________________________________________
___________________________________________________________________________
46
Sample Program B.
1. Type the following in a Notepad.
dosseg
.model small
.stack
.data
msg1 db 13,10,"Enter first number:$"
msg2 db 13,10,"Enter second number:$"
msg3 db 13,10,"Sum in decimal number:$"
num1 db ?
sum db ?
res db 20 DUP('$')
.code
main proc
movax,@data
movds,ax
lea dx,msg1
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,'0'
mov num1,al
lea dx,msg2
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,'0'
add al,num1
movsum,al
lea dx,msg3
mov ah,09h
int 21h
movsi,offset res
mov ax,00
moval,sum
call addition
lea dx,res
mov ah,09h
int 21h
47
mov ax,4c00h
int 21h
main endp
addition proc near
push ax
push bx
push cx
push dx
push si
mov cx,00h
mov bx,0Ah
add dl,'0'
push dx
inc cx
cmp ax,0Ah
jge rpt1
add al,'0'
mov [si],al
rpt2: pop ax
incsi
mov [si],al
loop rpt2
incsi
mov al,'$'
mov [si],al
pop si
pop dx
pop cx
pop bx
pop ax
ret
addition endp
end
2. Save the program as proc4.asm.
3. Assemble and execute the program.
4. Analyze the output and record the output in Table 7.4.
48
6. DATA ANALYSIS:
7. PROBLEMS:
1. Create an assembly language program asks the user to enter a password formed from 10
characters. The program prints the password as stars on the screen. If the password is correct, the
program should print “Password is CORRECT!” otherwise, “Password is INCORRECT!”
Create a program that will accept number in decimal and convert it to hexadecimal, binary and octal.
8. CONCLUSIONS:
49
9. Assessment (Rubric for Laboratory Performance):
50
Activity No. 8
ATTRIBUTES
Course Code: CPE005 Program:
Course Title: Computer Systems Organization with Assembly Date Performed:
Language
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to demonstrate cursor positioning and text-based attributes in Assembly.
The screen is typically has 80(0-79) columns x 25 (0-24). The columns and rows provide a grid of
addressable locations at any one of which the cursor can be set.INT 10h and Service 06h handles the
process of clearing the screen.
It is required to determine the position of X and Y coordinates where the next character is to be
displayed.INT 10H Service 02H sets the cursor position.
The attribute byte in text mode determines the characteristics of each displayed character. When a
program set an attribute, it remains set; that is , all subsequent displayed character have the same attribute
until one operation changes it. You can use INT 10H functions to generate a screen attribute and perform
such actions as scroll up or down, read attribute or character, or print / display attribute or character.
4. Resources:
Computer with 32-bit Operating System
TASM
5. Procedure:
Sample Problem A.
1. Type the following program in Notepad.
51
TITLE screen1.asm
.model small
.stack
.data
.code
start:
mov ah, 02
mov bh,0
mov dh, 10
mov dl,34
int 10h
mov ah,02
mov dl, 'T'
int 21h
mov ah,02
mov dl, 'I'
int 21h
2. Save the program as screen1.asm.
3. Assemble and execute mov ah,02 the program.
4. Analyze and record the mov dl, 'P' output in Table 8.1.
int 21h
Sample Problem B.
1. Modify program mov ah, 4ch screen1.asm, replace line
number 15 with “mov int 21h bh,0ch”.
2. Analyze and record the end start output in Table 8.2.
3. How is your output different from before? Why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Sample Problem C.
1. Modify program screen1.asm, replace line number 2 with “mov bh,1bh”.
2. Analyze and record the output in Table 8.3.
3. How is your output different from before? Why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
52
Sample Problem D.
1. Type the following program in Notepad.
TITLE screen2.asm
dosseg
.model small
.stack
.code
start:
mov ax, 0b800h
moves, ax
mov ah, 06h
mov cl, 00h
movch, 00h
mov dl, 18h
mov dh, 18h
movbh, 20h
int 10h
53
6. DATA ANALYSIS:
Table 8.1- Output of screen1.asm Table 8.2- Output of screen2.asm
7. PROBLEMS:
1. Modify screen2.asm. Change the background into four-color vertical “Light blue, yellow, magenta
and Cyan” and write down the name of the color on the space provided for every color.
Sample Output:
54
3. Make an assembly program that will display the figure shown below. Save your file as Football.asm
8. CONCLUSIONS
55
8. Assessment (Rubric for Laboratory Performance):
56
Activity No. 9
Parallel Port Interfacing
Course Code: CPE005 Program:
57
Course Title: Computer Systems Organization with Assembly Date Performed:
Language
Section: Date Submitted:
Name: Instructor:
1. Objective:
This activity aims to interface a circuit using a parallel port.
The parallel port is 25-pin, D-shaped socket found on personal computers. It is also called the
printer port because it is primary used to connect printers to computer. It is a simple and inexpensive tool
for building computer controlled devices and projects. Figure 9.1 shows the parallel port pin configuration.
The parallel port has three types of port namely: data port, status port and control port. The data
port is also called the output port which comprises pins 2 to 9. The status port is also called the input port
which comprises pins 10, 11, 12, 13, and 15. The control port is also called the input/ output (I/O) port
which comprises pins 1,14,16, and 17. Pins 18 through 25 are the ground pins.
The data port, status port and control port have address designations which are 378h, 379h (379h
(Base + 1) and 37Ah (Base + 2) respectively.
4. Resources:
Computer with 32-bit Operating System
TASM
58
5. Procedure:
1. Type the following in Notepad.
TITLE int1.asm
COMMENT !
A program that used to send logic 1’s to the DATA port pins and test the program.
This program when executed must satisfy the following conditions:
a. When user presses ‘1’ on the keyboard , the LEDs light from left to right
b. When user presses ‘2’ on the keyboard , the LEDs light from right to left
c. When user presses ‘0’ on the keyboard ,the LEDs light will turn OFF.
d. When user presses ESC the program exits.
!
dosseg
.model small
.code
start: call test
jmp start
test:
push cx
push ax
push dx
mov ah,06
mov dl,0ffh
int 21h
cmp al,27
je exit
cmp al,'1'
je left
cmp al,'2'
je right
cmp al,'0'
je off
pop dx
pop ax
pop cx
ret
59
jmp lop
delay: push cx
push ax
push dx
mov cx, 10h
mov dx,0
mov ah,86h
int 15h
pop dx
pop ax
pop cx
ret
4. Connect 8 LEDs to the data port of the parallel port as shown in the Figure 9.2.
60
Figure 9.2- Parallel Port Connection
6. DATA ANALYSIS:
61
Key Pressed LEDs ( 0-OFF) (1-ON)
D7 D6 D5 D4 D3 D2 D1 D0
0
1
2
Esc
7. PROBLEMS:
1. Connect a switch to any pin in the status port. Create a program that will light the LEDs “ON” if
the switch is ON, otherwise “OFF”.
2. Work with your classmate and design a circuit that will let you apply parallel port interfacing.
8. CONCLUSIONS:
62
63