Assembly Language
Homework 7
Exercise 1: Extended Subtraction Procedure
Create a procedure named Extended_Sub that subtracts two binary integers of arbitrary size.
•
Requirements: The procedure should accept pointers to two variables (the minuend and
the subtrahend) and a count of the number of bytes to subtract.
•
Hint: Use the SUB instruction for the first block and SBB (Subtract with Borrow) for
subsequent blocks.
Exercise 2: Bitwise Multiplication
Write a procedure named BitwiseMultiply that multiplies the value in EAX by an unsigned 32bit integer passed in EBX.
•
Requirements: Do not use the MUL or IMUL instructions. Instead, use a combination of
the SHL (Shift Left) and ADD instructions to perform the calculation.
•
Context: Shifting a number left by n bits multiplies it by 2^n.
Exercise 3: Add Packed Integers
Write a procedure named AddPacked that adds two packed decimal integers of arbitrary size.
•
Requirements: The integers will be stored in byte arrays. You must use the DAA
instruction to adjust the result after each byte addition to ensure the result remains in
valid packed decimal format.
Exercise 4: Encryption Using Rotate
Write a procedure that performs simple encryption on a string of bytes.
•
Requirements: Create an array of integers to act as an encryption key (e.g., key BYTE 2, 4, 1, 0...). Loop through a plaintext string and rotate the bits of each character. The
direction (left or right) and magnitude of the rotation should be determined by the
corresponding value in the key array.
Exercise 5: Greatest Common Divisor (GCD)
Implement the Euclidean algorithm to find the Greatest Common Divisor of two integers.
•
Requirements: Write a procedure named GCD that accepts two integers. Use the DIV
instruction to calculate the remainder repeatedly until the remainder is zero.
•
Algorithm:
int GCD(int x, int y) {
x = abs(x)
y = abs(y)
do {
int n = x % y
x=y
y=n
} while (y > 0)
return x
}