0% found this document useful (0 votes)
24 views17 pages

Digital System Engineering

Uploaded by

spider king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

Digital System Engineering

Uploaded by

spider king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Department of Electrical Engineering

EE 390
Digital Systems Engineering

Experiment No.2

Assembling Language Programming – 1

Submitted By:
Razan Farrj Al-Anazi
ID: 2211005101
Introduction:

Programming Assembly language considered a fundamental aspect of


understanding computer architecture and low-level programming. This experiment
aims to familiarize students with writing, assembling, and running assembly
language programs using the Turbo Assembler (TASM). Two primary methods are
commonly employed in assembly language programming: the full-segment
definition form and the simplified segment definition form. These methods involve
the use of real instructions (e.g., MOV and ADD) and pseudo instructions, which
guide the assembler in translating assembly language instructions into machine
code.

Objectives:

The objectives of this experiment are to:


 Understand the difference between real instructions and pseudo instructions.
 Gain hands-on experience in writing assembly language programs using
TASM.
 Generate and analyze map files to understand segment offset addresses in
the main assembly program.
 Learn how the assembler converts assembly language programs into
machine code.
 Assemble and run assembled programs to observe their execution.
 Familiarize oneself with the role of the linker in converting object files into
executable files.
Lab Work

Exercise 1:
Write, assemble, and run a program calculate sum and average of 5 bytes of data
And saves the result. The data should be the following hex numbers: 25H, 12H,
15H, 1FH, and 2BH. Use the simplified segment definition model.

The code
org 100h
.DATA
DATA DW 234DH.1DE6H.3BC7H.566AH
RESULT DW DUP<8>
.CODE
MOU AX.EDATA
MOU DS AX
LEA SI DATA
MOU AX. [SI]
MOU CX.3
NEXT:
ADD SI.2
ADD AX. [81]
LOOP NEXT
MOU RESULT.AX
HLT
ret
Exercise 2:
Assume that there is a class of five students with the following grades: 69, 87, 96,
45, and 75. Write and run an assembly program to find the highest grade and
Save it in certain memory location.

The code
.model small
.stack 100h

.data
grades db 69, 87, 96, 45, 75
num_students equ ($ - grades)

highest_grade db 0

.code
main proc
mov ax, @data
mov ds, ax

; Initialize highest grade with the first grade


mov al, grades
mov highest_grade, al

; Find the highest grade


mov cx, num_students
lea si, grades+1 ; Point to the second grade
next_grade:
mov al, [si]
cmp al, highest_grade
jg update_highest
continue_loop:
inc si
loop next_grade
jmp done

update_highest:
mov highest_grade, al
jmp continue_loop

done:
; Save the highest grade in memory location "highest_grade"
mov di, offset highest_grade
mov al, highest_grade
mov [di], al

; Save the highest grade in AX register


mov al, highest_grade
mov ah, 0

mov ah, 4ch


int 21h
main endp
end main
Exercise 3:
Assume that there is a class of five students with the following grades: 69, 87, 96,
45, and 75. Write and run an assembly program to find the lowest grade and
Save it in certain memory location.

The code
model small.
stack 100h.

data.
grades db 69, 87, 96, 45, 75
num_students equ ($ - grades)

lowest_grade db 255 ; Initialize with the highest possible grade

code.
main proc
mov ax, @data
mov ds, ax

Find the lowest grade ;


mov cx, num_students
lea si, grades+1 ; Point to the second grade
:next_grade
mov al, [si]
cmp al, lowest_grade
jl update_lowest
:continue_loop
inc si
loop next_grade
jmp done

:update_lowest
mov lowest_grade, al
jmp continue_loop

:done
"Save the lowest grade in memory location "lowest_grade ;
mov di, offset lowest_grade
mov al, lowest_grade
mov [di], al

Save the lowest grade in AX register ;


mov al, lowest_grade
mov ah, 0

mov ah, 4ch


int 21h
main endp
end main
Discussion

1. What is the purpose of pseudo-instructions, and how do they differ from


real instructions?

Pseudo-instructions are instructions in assembly language that are not actual


machine instructions but are used to define data, reserve memory, or provide
directives to the assembler. They help programmers manage memory allocation,
define constants, and control the assembly process. Pseudo-instructions differ
from real instructions in that real instructions are directly translated into
machine code, while pseudo-instructions are handled by the assembler during
the assembly process and may not directly translate to machine code.

2. What is the difference between the source file and the object file? Which
program converts Between them?
A source file is a text file containing the program written in a high-level
programming language or assembly language. It contains the human-readable
code that the programmer writes. An object file, on the other hand, is a file
generated by the assembler or compiler that contains machine code instructions
and data in a format that can be directly loaded into memory and executed by
the computer. The program that converts between them is called an assembler
or a compiler depending on whether the source language is assembly or a high-
level language.

3. What is the function of the linker program?

The linker program is responsible for combining multiple object files and
libraries into a single executable file. It resolves references between different
modules, ensuring that all symbols (functions and variables) are properly linked
and addresses are correctly resolved. The linker also performs memory
allocation for the final executable, arranging the code and data in memory in a
way that can be executed by the operating system.
4- Find the errors in the following assembly program and re-write it correctly
as Simplified Segment Format.

The errors in the provided assembly program include incorrect segment directives
and missing segment declarations.
Corrected program in Simplified Segment Format (SSF):

.MODEL SMALL

.DATA
X1 DB ?
X2 DB 30H

.CODE
MOV AX, @DATA
MOV DS, AX

MOV AL, 34H


ADD AL, 4FH
SUB AL, X2
MOV X1, AL

.EXIT
END
5. Write an assembly program that adds four words of data and save the
result in memory. Assume the values are: 234DH, 1DE6H, 3BC7H, and
566AH. Use the simplified segment Definition model.

. .MODEL SMALL

.DATA
NUMS DW 234DH, 1DE6H, 3BC7H, 566AH
RESULT DW ?

.CODE
MOV AX, @DATA
MOV DS, AX

MOV BX, OFFSET NUMS


MOV CX, 4 ; Number of words to add
MOV DX, 0 ; Initialize sum to 0

ADD_LOOP:
MOV AX, [BX] ; Load the next word into AX
ADD DX, AX ; Add AX to sum in DX
ADD BX, 2 ; Move to next word
LOOP ADD_LOOP ; Repeat for all words

MOV RESULT, DX ; Store the result in memory

.EXIT
END
6. Assume that there is a class of ten students with the following grades: 77,
46, 50, 36, 56, 68, 40, 63, 70, 29. Write a program to find the number of
passed students and save the result in Memory. Use the simplified segment
definition model.

.MODEL SMALL

.DATA
GRADES DB 77, 46, 50, 36, 56, 68, 40, 63, 70, 29
NUM_STUDENTS DB 10
NUM_PASSED DB ?

.CODE
MOV AX, @DATA
MOV DS, AX

MOV CX, NUM_STUDENTS ; Number of students


MOV SI, OFFSET GRADES ; Point to the first grade
MOV BL, 0 ; Initialize counter for passed students to 0

COUNT_PASSED:
MOV AL, [SI] ; Load the grade
CMP AL, 50 ; Compare with passing grade
JAE IS_PASSED ; Jump if grade is greater than or equal to 50
JMP NOT_PASSED

IS_PASSED:
INC BL ; Increment counter for passed students

NOT_PASSED:
INC SI ; Move to the next grade
LOOP COUNT_PASSED ; Repeat for all students

MOV NUM_PASSED, BL ; Store the number of passed students in memory


.EXIT
END

7. What is the difference between the following two instructions:


(a) MOV BX,DATA1
(b) MOV BX,OFFSET DATA1

The difference between the two instructions is the value that is loaded into the
`BX` register:

1. `MOV BX, DATA1`: This instruction moves the value stored at the memory
address labeled `DATA1` into the `BX` register. It treats `DATA1` as a
memory operand, so the value loaded into `BX` is the content of the memory
location labeled `DATA1`.

2. `MOV BX, OFFSET DATA1`: This instruction moves the offset of the memory
address labeled `DATA1` into the `BX` register. It does not access the content
of `DATA1`, but rather the location or address where `DATA1` is stored in
memory. This is useful when you want to get the address of a variable or label
for use in addressing calculations, such as accessing elements of an array or
jumping to a label in the code.
8. How many bytes are defined by the following pseudo instruction? DATA1
DB 6 DUP (4 DUP(0FFH))

The pseudo-instruction `DATA1 DB 6 DUP (4 DUP(0FFH))` can be expanded to


understand the total number of bytes it defines:

- `4 DUP (0FFH)` defines an array of 4 bytes, each initialized to `0FFH`. So, this
part defines 4 bytes.
- `6 DUP (4 DUP(0FFH))` defines an array of 6 elements, each element being the
array defined in the previous step. So, this part defines 6 * 4 = 24 bytes.
Therefore, the total number of bytes defined by `DATA1 DB 6 DUP (4
DUP(0FFH))` is 24 bytes.
Conclusion
This lab experiment focused on learning the basics of writing,

assembling, and running assembly language programs using the Turbo

Assembler (TASM). The experiment highlighted the two methods of

writing assembly programs: the full-segment definition form and the

simplified segment definition form. It also discussed the role of real

instructions and pseudo instructions, where real instructions are

translated into machine code for execution, while pseudo instructions are

used to provide directives to the assembler.

You might also like