Task: Multiplying Two Numbers
Multiplying Two Numbers represented as Bytes
You will solve this exercise starting from the multiply.asm file located in the tasks/mul/support directory.
Go through, run, and test the code from the file multiply.asm. In this program, we multiply two numbers defined as bytes. To access them, we use a construction like byte [register].
When performing multiplication, the process is as follows, as described here:
- We place the multiplicand in the multiplicand register, meaning:
- if we’re operating on a byte (8 bits, one byte), we place the multiplicand in the
alregister; - if we’re operating on a word (16 bits, 2 bytes), we place the multiplicand in the
axregister; - if we’re operating on a double word (32 bits, 4 bytes), we place the multiplicand in the
eaxregister.
- if we’re operating on a byte (8 bits, one byte), we place the multiplicand in the
- The multiplier is passed as an argument to the
mulmnemonic. The multiplier must have the same size as the multiplicand. - The result is placed in two registers (the high part and the low part).
Multiplying Two Numbers represented as Words / Double Words
Update the area marked with TODO in the file multiply.asm to allow multiplication of word and dword numbers, namely num1_dw with num2_dw, and num1_dd with num2_dd.
TIP: For multiplying word numbers (16 bits), the components are arranged as follows:
- Place the multiplicand in the
axregister.- The argument of the
mulinstruction, the multiplier (possibly another register), is 16 bits (either a value or a register such asbx,cx,dx).- The result of the multiplication is arranged in the pair
dx:ax, where the high part of the result is in thedxregister, and the low part of the result is in theaxregister.For multiplying
dwordnumbers (32 bits), the components are arranged as follows:
- Place the multiplicand in the
eaxregister.- The argument of the
mulinstruction, the multiplier (possibly another register), is 32 bits (either a value or a register such asebx,ecx,edx).- The result of the multiplication is arranged in the pair
edx:eax, where the high part of the result is in theedxregister, and the low part of the result is in theeaxregister.NOTE: When displaying the result, use the
PRINTF32macro to display the two registers containing the result:
- Registers
dxandaxfor multiplying word numbers.- Registers
edxandeaxfor multiplying dword numbers.
Testing
To test the implementation, enter the tests/ directory and run:
make check
In case of a correct solution, you will get an output such as:
test_byte_mul ........................ passed ... 33
test_short_mul ........................ passed ... 33
test_int_mul ........................ passed ... 34
========================================================================
Total: 100/100
If you’re having difficulties solving this exercise, go through this reading material.