Você está na página 1de 3

Assembly Language Programming D

FAST-NU, Lahore, Spring 2018

Homework 3

Due Monday March 26 11:55 P.M. Marked out of 100 points.

Write code for the following problems. Submit soft copies via SLATE.
• In each of the following you are supposed to write a function with the specified
parameters to perform the specified task.
• Make sure that all these functions preserve the register contents and empty the
stack from parameters after return.

1. Write a function called print_char which accepts a single word (16 bit) input in
parameter whose lower 8-bits contain the ASCII of the character to be printed
and higher 8-bits are zero, and a 16-bit offset value. The function should then
print the character whose ASCII has been provided by moving it to the specified
offset on the video memory, which starts at 0xB8000.
The following code snippet explains how to print the character ‘A’ at offset si. The
ASCII of ‘A’ is 0x41. You will have to move a whole word (16 bits) to the video
memory where the lower byte of the word is the ASCII and the higher byte is
fixed to 0x07; therefore to print ‘A’ you will move 0x0741 to the video memory.

mov ax, 0xB800


mov es, ax ;now es points to the video segment at physical address 0xB8000
mov [es:si], ax ;prints ‘A’ on the screen

2. Write a function called print_str which accepts the address of a null-terminated


character array in parameter and prints the entire array by using the function
print_char.
3. Write a function called binary_search which accepts the pointer of a sorted word
array, its size, a word sized key, and a pointer to a word-sized flag in parameters.
The function should set the word flag if the key is found in the array and clear it
otherwise. Obviously, the function should implement the binary search algorithm.
4. Write a function called intersect which accepts the pointers of two word arrays
containing non-negative numbers terminated by -1. The function should copy
their common elements into a third array whose pointer is also passed as a
parameter. The two arrays are sorted, so intersect should use binary_search to
check if an element of one array exists in the other. The resultant array should
also be terminated by a -1.
5. The number C(n, r) is defined as: the number of ways to select r things from n
things where the order of selection does not matter. For example, the number of
ways to select 1 student from a group 3 students is C(3, 1) = 3, also, C(3, 2) = 3,
and C(4, 2) = 6 etc. Please confirm this by counting.
One interesting property of C(n, r) is that, C(n, r) = C(n-1, r) + C(n-1, r-1). For
example, C(4, 2) = C(3, 2) + C(3, 1) = 3+3=6.
Why is this true?
Here is the reason: Let’s say that you want to select r things from n things where
one of these n things is x. Now there are only two possible scenarios: either your
r things will contain x, or they will not contain x.
So C(n, r) = #of ways of selecting r things with x excluded from selection +
#of ways of selecting r things with x included in selection
But,
#of ways of selecting r things with x excluded from selection = C(n-1, r)
because you exclude x and select all r things from the remaining n-1 things.
and,
#of ways of selecting r things with x included in selection = C(n-1, r-1)
because x is already selected, you select the remaining r-1 things from the
remaining n-1 things.
Therefore, C(n, r) = C(n-1, r) + C(n-1, r-1).
Anyway, now that you understand why this recursive definition of C(n, r) is true,
write a recursive function c_n_r which accepts the values of n and r as
parameters and returns the result in the ax register.
Note that for base cases, C(n, 0) = 0, C(n, 1) = 1, and C(0, x) = 0, etc.
6. This question is from your book.
Write a function called switch_stack meant to change the current stack and will
be called as below:
push word [new_stack_segment]
push word [new_stack_offset]
call switch_stack

What does it mean to switch the stack? The stack is defined by the SS and SP
registers, the former containing the segment base and the latter the offset of the top of
the stack. Changing the stack would mean that both SS and SP are loaded with new
values. However, while doing so, you must be careful that the function itself is using the
stack in push and ret instructions. It must remove all the preserved registers, the return
address and parameters from the older stack as all functions do. When it returns to the
caller the transition should be smooth, and the new stack should be in use.
7. Here’s another problem from your book. Make an array of 0x80 bytes and treat it
as one of 0x400 bits. Write a function myalloc that takes one argument, the
number of bits. It finds that many consecutive zero bits in the array, makes them
one, and returns in AX the index of the first bit. Write another function myfree that
takes two arguments, index of a bit in the array, and the number of bits. It makes
that many consecutive bits zero, whatever their previous values are, starting from
the index in the first argument.

THE END

Você também pode gostar