Coal Assignment 01 Solved
Coal Assignment 01 Solved
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
tsize DWORD 4
dsize DWORD 8
var dword 0
newline db 13, 10
.code
start:
add esi, 8
mov eax, 0
mov ebx, 0
loop swap_loop
mov esi, 0
output_loop:
add esi, 4
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
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
buffer dword 0
var dword 0
newline db 13, 10
.code
start:
mov eax, 0
mov [littleEndian], al
mov ecx , 4
mov eax, 0
mov esi, 0
L1:
inc esi
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 result is converted to a string and displayed with the label "EAX: ".
2. EDX Operations:
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)
var dword 0
newline db 13, 10
.code
start:
mov eax, 0
add eax, 5
mov edx, 5
add edx, 5
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
buffer dword 0
newline db 13, 10
.code
start:
mov ecx, arraylength
dec ecx
mov eax, 0
loop_start:
add esi, 4
loop loop_start
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
arraylength dword 4
buffer dword 0
newline db 13, 10
.code
start:
movzx eax, ax
add esi, 2
add edi, 4
loop copy
output:
add esi, 4
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 ,
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
count dword 5
buffer dword 0
newline db 13, 10
.code
start:
mov esi, OFFSET array
mov eax, 1
mov ebx, 1
mov edx,eax
mov eax,1
mov ebx,1
mov count, 0
add esi, 8
calculate_fib:
add esi, 4
loop calculate_fib
mov ecx, 7
output:
add esi, 4
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
buffer db 12 dup(0)
count dword 0
newline db 13, 10
.code
start:
sub ecx, 2
mov ebx, 0
reverse:
loop reverse
output:
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:
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
.code
start:
dec ecx
dec edi
copy_reverse:
mov [edi], al
inc esi
dec edi
loop copy_reverse
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
result db 50 dup(0)
.code
start:
dec ecx
rotate:
dec esi
loop rotate
mov esi, 0
OUTPUT:
inc esi
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
start:
movzx eax, ax
movzx eax, al
movzx eax, ax
end start
Output: