0% found this document useful (0 votes)
29 views

Coal Assignment 01 Solved

Uploaded by

Anila Younas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Coal Assignment 01 Solved

Uploaded by

Anila Younas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Namal University Mianwali

Course: Computer Organization & Assembly Language


Course code: CSC-241L
Assignment 01

Submitted by Anila Younas


Roll No. NUM-BSCS-2023-05
Submitted to Mr. Muzamil Ahmed
Date 06/11/2024
Task 01
You’re working on a program to manage a data buffer that temporarily holds packets
of sensor data in a connected device. The buffer is represented as an array with an even
number of values, each representing a different packet. Due to a requirement from the
communication protocol, you need to exchange every pair of packets before the data is
processed further. Specifically, the packet in position i should swap places with the
packet in position i+1, the packet in position i+2 should swap with the one in position
i+3, and so on, until all pairs have been exchanged. Write a program that utilizes a loop
and indexed addressing to perform this task. The program should assume the buffer
always contains an even number of packets.

Explanation:
In this program, the swapping of each adjacent array elements is done in the code below. Using
indexed addressing, the program accesses two adjacent elements at a time as shown below: [esi], and
[esi+4]. It swaps the last and the first; afterwards moves to the second with its adjacent element and
swap for example, if one will try to swap the number as in the following figure with each element in
the array; using the indexed addressing that provides direct access to these required data. This ensures
that the two packets that are next to one another in the buffer are swapped if necessary.

Code:

include \masm32\include\masm32rt.inc

.data

array DWORD 10, 20, 30, 40, 50, 60, 70, 80 ,0

buffer db 11 dup (0)

tsize DWORD 4

dsize DWORD 8

msg db "Array After Exchange: ", 0

var dword 0

newline db 13, 10

.code

start:

mov ecx, tsize

mov esi, OFFSET array


swap_loop:

mov var, ecx

mov eax, [esi]

mov ebx, [esi + 4]

mov [esi], ebx

mov [esi + 4], eax

add esi, 8

mov eax, 0

mov ebx, 0

mov ecx, var

loop swap_loop

invoke StdOut, addr msg

invoke StdOut, addr newline

mov esi, 0

mov ecx, dsize

output_loop:

mov var, ecx

mov ebx, array[esi]

mov eax, ebx

invoke dwtoa, eax, addr buffer


invoke StdOut, addr buffer

invoke StdOut, addr newline

add esi, 4

mov ecx, var

loop output_loop

invoke ExitProcess, 0

end start

Output:

Task 02
In a systems programming course, you are given a task to prepare data for an
embedded system that processes numeric values. The system uses a 32-bit hexadecimal
number represented in a big-endian format. The value 0x12345678 is stored in the
variable bigEndian as follows:

.data

bigEndian BYTE 12h,34h,56h,78h

littleEndian DWORD?

However, the processing unit of the embedded system requires this value to be in little-
endian format, where the byte order is reversed. Your objective is to write a program
that utilizes MOV instructions to copy the value from bigEndian to littleEndian,
reversing the order of the bytes. Considering the constraints of the system, how would
you implement this byte reversal in your program? Be sure to detail the steps you take
in your assembly code to achieve the desired outcome.

Explanation:
In this program, I am swapping the byte sequence of bigEndian to littleEndian. The
bytes of bigEndian are fed one by one into littleEndian in backward order; that is, from the last byte of
bigEndian to the first. Once byte order is swapped then littleEndian is printed, and the bytes are
represented in little-endian format. This program is using a loop that prints each individual byte.

Code:

include \masm32\include\masm32rt.inc

.data

bigEndian byte 12h, 34h, 56h , 78h

littleEndian byte 4 dup(0)

littlemsg db "In little Endian Order: ", 0

buffer dword 0

var dword 0

newline db 13, 10

.code

start:

mov eax, 0

mov al, [bigEndian]

mov [littleEndian + 3], al

movzx eax, [littleEndian + 3]

mov al, [bigEndian + 1]

mov [littleEndian + 2], al

movzx ebx, [littleEndian + 2]


mov al, [bigEndian + 2]

mov [littleEndian + 1], al

movzx ecx, [littleEndian + 1]

mov al, [bigEndian + 3]

mov [littleEndian], al

movzx edx, [littleEndian]

invoke StdOut, addr littlemsg

invoke StdOut, addr newline

mov ecx , 4

mov eax, 0

mov esi, 0

L1:

mov var, ecx

movzx ebx, [littleEndian +esi]

invoke dwtoa, ebx, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

inc esi

mov ecx, var

loop L1

invoke ExitProcess, 0

end start
Output:

Task 03
Write a program that contains two instructions: (1) add the
number 5 to the EAX register, and (2) add 5 to the EDX register.
Generate a listing file and examine the machine code generated
by the assembler. What differences, if any, did you find between
the two instructions?

Explanation:
1. EAX Operations:

 The value 0 is loaded into EAX, then 5 is added, resulting in 5.

 The result is converted to a string and displayed with the label "EAX: ".

2. EDX Operations:

 The value 5 is loaded into EDX, then 5 is added, resulting in 10.

 The result is converted to a string and displayed with the label "EDX: ".

3. Output:
 The program outputs "EAX: 5" and "EDX: 10".
4. Listing File:
 I listed the file using ml/ c/Fl task3.asm command in PowerShell. This command
created a listing file of task 3 in the VS Code. The listing file contained all the
information and machine code of the assembly program being used. Both the registers
gave the correct value.

Code:

include \masm32\include\masm32rt.inc

.data
buffer db 11 dup (0)

msg1 db "EAX: ", 0

msg2 db "EDX: ", 0

var dword 0

newline db 13, 10

.code

start:

mov eax, 0

add eax, 5

invoke StdOut, addr msg1

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

mov edx, 5

add edx, 5

invoke dwtoa, edx, addr buffer

invoke StdOut, addr msg2

invoke StdOut, addr buffer

invoke StdOut, addr newline

invoke ExitProcess, 0

end start

Output:
Task 04
Write a program with a loop and indexed addressing that
calculates the sum of all the gaps between successive array
elements. The array elements are doublewords, sequenced in
nondecreasing order. So, for example, the array {0, 2, 5, 9, 10}
has gaps of 2, 3, 4, and 1, whose sum equals 10.

Explanation:
In this program, I calculated the sum of gaps between adjacent array elements. From the
second element, I subtract the first from the second, then the second from the third, and so on.
All those results are summed up in the eax register. At the end of the loop, the sum of all
those gaps is laid down in the gap_sum. Finally, I convert the sum to a string and write it to
StdOut.

Code:

include \masm32\include\masm32rt.inc

.data

array dword 0, 2, 5, 9, 10

arraylength dword 5

gap_sum dword 0

sum db "Sum: " , 0

buffer dword 0

newline db 13, 10

.code

start:
mov ecx, arraylength

dec ecx

mov esi, offset array

mov eax, 0

loop_start:

mov edx, [esi + 4]

sub edx, [esi]

add eax, edx

add esi, 4

loop loop_start

mov [gap_sum], eax

invoke dwtoa, eax, addr buffer

invoke StdOut, addr sum

invoke StdOut, addr buffer

invoke StdOut, addr newline

invoke ExitProcess, 0

end start

Output:
Task 05
Write a program that uses a loop to copy all the elements from an
unsigned Word (16-bit) array into an unsigned doubleword (32-
bit) array.

Explanation:
In this code, I copy values from a 16-bit array to a 32-bit array. Arrayone contains word-sized
values, and I have to move these into arraytwo that hold double the size of a word, or 32-bit
values. In order to do that, I load each 16-bit value from arrayone and then zero extend it into
a 32-bit value using movzx, then store the result in arraytwo. This results in the esi register
being pointed at arrayone, and the edi register pointing to arraytwo, for which the program
increments those addresses appropriately in order to proceed through both arrays.

After copying, I print contents of arraytwo. Inside that loop, for every 32-bit, I convert it into
a string using dwtoa and print that using StdOut.

Code:

include \masm32\include\masm32rt.inc

.data

arrayone word 10, 20, 30, 40

arraylength dword 4

arraytwo dword 4 dup(0)

buffer dword 0

newline db 13, 10

.code

start:

mov esi, OFFSET arrayone

mov edi, OFFSET arraytwo

mov ecx, arraylength


copy:

mov ax, [esi]

movzx eax, ax

mov [edi], eax

add esi, 2

add edi, 4

loop copy

mov esi, OFFSET arraytwo

mov ecx, arraylength

output:

mov ebx, ecx

mov eax, [esi]

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

add esi, 4

mov ecx, ebx

loop output

invoke ExitProcess, 0

end start

Output:
Task 06
Write a program that uses a loop to calculate the first seven values of the Fibonacci
number sequence, described by the following formula:

Fib ( 1 )=1 ,

Fib ( 2 )=1 ,

Fib(n)=Fib(n – 1)+ Fib (n – 2)

Explanation:
The program computes the first seven Fibonacci numbers and writes them into an array. It
initializes the first two Fibonacci numbers as 1 and 1, respectively, and then it starts
computing the following ones based on the formula of the Fibonacci numbers (F(n) = F(n-1)
+ F(n-2)). The program iterates over the array inside a loop, writes every number as a string,
and prints it. The registers used are eax, ebx, and edx to store the Fibonacci values, esi for
array indexing, and ecx to control the loop count.

Code:

include \masm32\include\masm32rt.inc

.data

array dword 7 dup(0)

count dword 5

buffer dword 0

newline db 13, 10

.code

start:
mov esi, OFFSET array

mov eax, 1

mov ebx, 1

mov [esi], eax

mov [esi+4], ebx

mov edx,eax

mov ecx, count

mov eax,1

mov ebx,1

mov count, 0

add esi, 8

calculate_fib:

mov count, ecx

add eax, ebx

mov [esi], eax

mov edx, eax

mov eax, ebx

mov ebx, edx

add esi, 4

mov ecx, count

loop calculate_fib

mov esi, OFFSET array

mov ecx, 7

output:

mov count, ecx


mov eax, [esi]

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

add esi, 4

mov ecx, count

loop output

invoke ExitProcess, 0

end start

Output:

Task 07
Use a loop with indirect or indexed addressing to reverse the
elements of an integer array in place. Do not copy the elements
to any other array. Use the SIZEOF, TYPE, and LENGTHOF
operators to make the program as flexible as possible if the array
size and type should be changed in the future.

Explanation:
This is a reversing of elements in an array program. The program starts off by
getting the size of the array, then creates two pointers to point to the start of the
array and the last element in the array: esi and edi, respectively. It enters a
loop, swapping elements in between those two pointers towards the center of the
array. Lastly, it prints out the elements in the array on the console. There are eax and
edx, registers, used for temporarily holding values to be swapped; an index is kept by ebx, a l
oop by ecx, and there's dwtoa function which turns integers into strings to output.

Code:

include \masm32\include\masm32rt.inc

.data

array dword 10, 20, 30, 40, 50

buffer db 12 dup(0)

count dword 0

newline db 13, 10

.code

start:

mov ecx, LENGTHOF array

sub ecx, 2

mov ebx, 0

mov esi, OFFSET array

mov edi, OFFSET array

add edi, SIZEOF array

sub edi, SIZEOF dword

reverse:

mov count, ecx

mov eax, [esi + ebx * TYPE array]

mov edx, [edi]

mov [esi + ebx * TYPE array], edx

mov [edi], eax


inc ebx

sub edi, SIZEOF dword

mov ecx, count

loop reverse

mov ecx, LENGTHOF array

mov esi, OFFSET array

output:

mov count, ecx

mov eax, [esi]

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

add esi, SIZEOF dword

mov ecx, count

loop output

invoke ExitProcess, 0

end start

Output:

Task 08
Write a program with a loop and indirect addressing that copies a string from source to
target, reversing the character order in the process. Use the following variables:

source BYTE "This is the source string",0

target BYTE SIZEOF source DUP('#')

Explanation:
This program copies a string from source to target in reverse order. It uses
pointers, namely esi for the source and edi for the target. The program calculates the length of
the strings and then copies every character from source to target beginning at the end of the
target array. It finishes the target string with a null character and prints out the reversed string.

Code:

include \masm32\include\masm32rt.inc

.data

source BYTE "This is the source string", 0

target BYTE SIZEOF source DUP(0)

newline db 13, 10, 0

.code

start:

mov esi, OFFSET source

mov edi, OFFSET target

mov ecx, LENGTHOF source

dec ecx

add edi, ecx

dec edi
copy_reverse:

mov al, [esi]

mov [edi], al

inc esi

dec edi

loop copy_reverse

mov byte ptr [edi], 0

invoke StdOut, OFFSET target

invoke StdOut, addr newline

invoke ExitProcess, 0

end start

Output:

Task 09
Using a loop and indexed addressing, write code that rotates the
members of a 32-bit integer array forward one position. The value
at the end of the array must wrap around to the first position. For
example, the array [10,20,30,40] would be transformed into
[40,10,20,30].

Explanation:
This program rotates the elements in an array in a circle. It puts the last element in the
register EAX and then shifts each one to the right. A first element is put on the last position.
After moving the elements around, this program prints the modified array. The rotation
is affected by a loop that causes the movement of elements in an array, and
the print statement is done after the elements have been rotated.

Code:

include \masm32\include\masm32rt.inc

.data

array DWORD 10, 20, 30, 40

count DWORD LENGTHOF array

result db 50 dup(0)

newline db 13, 10, 0

buffer db "Output: ", 20 dup(0)

.code

start:

mov ecx, count

dec ecx

mov eax, [array + ecx * 4]

mov esi, ecx

rotate:

mov ebx, [array + esi * 4 - 4]

mov [array + esi * 4], ebx

dec esi

loop rotate

mov [array], eax


invoke StdOut, addr buffer

invoke StdOut, addr newline

mov ecx, count

mov esi, 0

OUTPUT:

mov ebx, ecx

invoke dwtoa, [array + esi * 4], addr result

invoke StdOut, addr result

invoke StdOut, addr newline

inc esi

mov ecx, ebx

loop OUTPUT

invoke ExitProcess, 0

END start

Output:

Task 10
1. Write a single instruction that moves the first two bytes in
myBytes to the DX register. The resulting value will be
2010h.
2. Write an instruction that moves the second byte in myWords
to the AL register.
3. Write an instruction that moves all four bytes in myBytes to
the EAX register.
4. Insert a LABEL directive in the given data that permits
myWords to be moved directly to a 32-bit register.
5. Insert a LABEL directive in the given data that permits
myBytes to be moved directly to a 16-bit register.

Explanation:
 Accessing myBytes: The program loads the first word (2 bytes) from the myBytes
array, which contains the values 10h and 20h. It then converts this word to its decimal
equivalent and prints it.
 Accessing myWords: The program loads the byte at index 2 of the myWords array,
which is 00h (due to the uninitialized values) and prints its decimal equivalent, which
is 0.
 Accessing the DWORD value in myBytes: The program loads the first 4 bytes from
the myBytes array (which are 10h, 20h, 30h, 40h), forming the DWORD value
0x40302010. It converts this to its decimal equivalent and prints it.
 Accessing myBytesLabel: The program tries to load the word from myBytesLabel,
which prints 8208.

Code:

include \masm32\include\masm32rt.inc

.data

myBytesLabel label WORD

myBytes BYTE 10h, 20h, 30h, 40h

myWordsLabel label DWORD

myWords WORD 3 DUP(?), 2000h

myString BYTE "ABCDE"

buffer dword 32 dup(0), 0

newline db 13, 10, 0


.code

start:

mov ax, word ptr myBytes

movzx eax, ax

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

mov al, byte ptr [myWords + 2]

movzx eax, al

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

mov eax, dword ptr myBytes

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline

mov ax, word ptr myBytesLabel

movzx eax, ax

invoke dwtoa, eax, addr buffer

invoke StdOut, addr buffer

invoke StdOut, addr newline


invoke ExitProcess, 0

end start

Output:

You might also like