Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Name: Kathlene kaye S.

Cleto Date: May 17, 2024


Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

Activity:

1. Understanding mov Instruction:

 Explain the purpose of the mov instruction in assembly language.

- The "mov" instruction holds significant importance within assembly language


and is widely utilized. Its primary function entails the transfer of data from
one location to another by copying the data from a source operand to a
destination operand. Notably, the mov instruction preserves the data in the
source operand, solely duplicating it to the destination operand.

 How is data transferred between registers or memory locations using mov?

- The mov instruction can transfer data from a memory location to a register and vice
versa. An example of this is mov eax, [ebx], which copies the data from the
memory address contained in ebx into the eax register. Conversely, mov [ebx], eax
stores the data from eax into the memory location pointed to by ebx. Additionally, it
can move an immediate value directly into a memory location, demonstrated by mov
[ebx], 10, which stores the value 10 at the memory address in ebx.

- The operands used in mov instructions can be registers, immediate values, or


memory addresses, with various addressing modes available, such as indirect
addressing using registers (e.g., [ebx], [esi+4]). By enabling the transfer of data
between these different types of operands, the mov instruction plays a crucial role in
setting up values in registers, initializing memory, and performing a broad range of
data manipulation tasks essential for programming in assembly language.
Understanding how to effectively utilize the mov instruction is key to mastering
assembly language.

 Provide an example of using mov to move an immediate value into a register.


Sample code:
Mov eax, 8 ; Move the immediate value 8 into register eax
Explanation:
Mov: This is the instruction that performs the move operation.
Eax: This is the register where the immediate value will be stored. In this case, we’re
using the general-purpose register eax.
8: This is the immediate value (a constant) that will be copied into the register.
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

2. Basic Arithmetic with add and sub:

 Describe the difference between the add and sub instructions in assembly language.

- Both `add` and `sub` instructions perform arithmetic operations in assembly


language. The `add` instruction adds two operands and stores the result in a
register. The operands for this instruction can be a combination of a register
and an immediate value (constant), two registers, or a register and a memory
location. Similarly, the `sub` instruction subtracts the second operand from the
first operand and stores the result in a register. Like the `add` instruction, the
operands for `sub` can be registers, immediate values, or memory locations.

 How are add and sub used for arithmetic operations on registers or memory
locations?

- The `add` and `sub` instructions in emu8086, an emulator for the x86
architecture, are versatile tools for performing arithmetic on registers and
memory locations.

- When using registers, the `add` instruction can add the values of two registers
and store the result in the first register. For example, `add eax, ebx` adds the
contents of `ebx` to `eax`, storing the sum in `eax` and overwriting its original
value. Similarly, you can add an immediate value (a constant) to a register,
such as in `add eax, 5`, which adds 5 to the contents of `eax`.

- For operations involving memory locations, the `add` instruction can also be
used to add the content of a memory location to a register. For instance, `add
eax, [bx]` adds the value at the memory location pointed to by `bx` to `eax`,
storing the result back in `eax`. Adding an immediate value directly to a
memory location requires two steps in emu8086. First, you move the
immediate value to a register (`mov edx, 10`), and then add this register value
to the memory location (`add [di], edx`).
- The `sub` instruction operates similarly to `add`, but it subtracts the second
operand from the first and stores the result in the first operand. Like `add`,
`sub` can use registers, immediate values, or memory locations as operands.
The key points to remember are that the first operand usually specifies where
the result will be stored (typically a register), and the second operand can be
another register, an immediate value, or a memory location. Both instructions
also affect the processor’s flags, which are useful for further calculations and
decision-making processes.
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

- Understanding these concepts allows you to effectively utilize `add` and `sub`
instructions for various arithmetic operations on registers and memory
locations in your emu8086 programs.

 Show a simple assembly code snippet using add to add two numbers stored in
registers.

Simple assembly code snippets:

Mov eax, 5 ; Move the immediate value 5 to register eax


Mov ebx, 8 ; Move the immediate value 8 to register ebx
Add eax, ebx ; Add the content of ebx to eax, store the sum in eax

; Optional: Print the result (specific implementation depends on your emulator)


; … print eax

; Rest of your program code

Here’s a simple assembly code snippet using `add` to add two numbers stored in
registers:

```assembly
Mov eax, 5 ; Move the immediate value 5 to register eax
Mov ebx, 8 ; Move the immediate value 8 to register ebx
Add eax, ebx ; Add the content of ebx to eax, store the sum in eax

; Optional: Print the result (specific implementation depends on your emulator)


; … print eax

; Rest of your program code


```

Explanation:

a. We first initialize two registers:


`mov eax, 5`: Moves the immediate value 5 into register `eax`.
`mov ebx, 8`: Moves the immediate value 8 into register `ebx`.
b. The `add` instruction comes next:
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

`add eax, ebx`: This adds the content of register `ebx` (which is 8) to the
content of register `eax` (which is 5). The result (13) is stored back in register
`eax`. The original value in `eax` (5) is overwritten.
c. An optional section shows how you might print the result using emulator-
specific methods (printing functionality might vary).
d. The rest of your program code can use the value in `eax` (which now holds the
sum 13) for further calculations or operations.

3. Using mov, add, and sub Together:

 Write an assembly code segment that loads a constant value into a register using mov,
adds another value to it using add, and then subtracts a third value using sub.

Here’s an assembly code segment that demonstrates using `mov`, `add`, and `sub`
together:

```assembly
Mov eax, 10 ; Move the immediate value 10 into register eax
Add eax, 5 ; Add the immediate value 5 to eax, store the sum in eax (now 15)
Sub eax, 3 ; Subtract the immediate value 3 from eax, store the difference in
eax (now 12)

; Optional: Print the result (specific implementation depends on your emulator)


; … print eax

; Rest of your program code


```

Explanation:

a. `mov eax, 10`: Loads the constant value 10 into register `eax`.
b. `add eax, 5`: Adds the immediate value 5 to the current value in `eax` (which is
10). The sum (15) is stored back in `eax`.
c. `sub eax, 3`: Subtracts the immediate value 3 from the current value in `eax`
(which is 15). The difference (12) is stored back in `eax`.

This code demonstrates a sequence of operations: loading a constant, adding another


value, and then subtracting a third value. The final result (12) is stored in register
`eax`.
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

The optional section shows how you might print the result using emulator-specific
methods. The rest of your program code can then use the final value in `eax` (12) for
further calculations or operations.

 What are the typical limitations or restrictions when using add or sub instructions in
assembly?

- While `add` and `sub` instructions are fundamental for arithmetic in assembly
language, they come with certain limitations. The size of numbers you can add
or subtract depends on the architecture. Calculations might overflow or
underflow if the result is too large or too small for the register being used.
These conditions are indicated by flags, and your program needs to check
these flags to avoid incorrect results. Additionally, `add` can set a carry flag if
the sum doesn’t fit in the register, and `sub` can affect another flag based on
whether borrowing was required during the subtraction.

- There might also be limitations on how you can use these instructions with
memory locations. In some cases, you might need separate instructions to
move a constant value to memory before adding or subtracting it. Finally,
some processors might have dedicated instructions for specific arithmetic
operations like multiplication or division, which can be more efficient than
using multiple `add` or `sub` instructions. It’s important to consider these
limitations and the specific behavior of your emulator when writing assembly
code to ensure accurate and efficient calculations.

4. Memory Operations:

 Explain how memory addressing works with mov, add, and sub.

- In assembly language, memory addressing is essential for using instructions


like `mov`, `add`, and `sub` to work with data stored in memory. These
instructions rely on different addressing modes to specify how the processor
locates the data. Common modes include using a register directly (e.g., `mov
eax, [ebx]`), adding a constant value to a base register (e.g., `add eax, [bx +
4]`), or using a register that holds the memory address itself (e.g., `sub eax,
[esi]`).

- `mov` utilizes memory addressing for both loading data into registers and
storing data from registers. The addressing mode tells the processor where to
find the data in memory. For example, `mov eax, [100]` (assuming a valid
addressing mode) retrieves the value at memory location 100 and puts it in
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

`eax`. Conversely, `mov [bx], ecx` stores the value from `ecx` into the
memory location pointed to by `bx`.

- Both `add` and `sub` can work with memory operands. The first operand
(destination) is usually a register where the result will be stored. The second
operand (source) can be another register or a memory location identified using
an addressing mode. Examples include `add eax, [esi]` (which adds the value
at the memory location pointed to by `esi` to `eax`) and `sub eax, [ebx + 8]`
(which subtracts the value at the memory location 8 bytes beyond the address
in `ebx` from `eax`).

- Understanding these addressing modes is crucial for working with data in


memory using `mov`, `add`, and `sub` effectively. Keep in mind that some
assembly languages might have limitations on which addressing modes can be
used with these instructions. By mastering memory addressing, you can write
assembly code that interacts with memory and performs calculations on the
data stored there.

 Give an example of loading data from memory into a register using mov.

- Example of loading data from memory into a register using mov in assembly
language:

mov eax, [ebx]

Breakdown operation:

Mov: This is the instruction that performs the move operation.


Eax: This is the register where the data will be loaded. In this case, we’re
using the general-purpose register eax.
[ebx]: This specifies the memory location from where the data will be loaded.
The square brackets [] indicate that we’re using an addressing mode. Here,
we’re using the register direct addressing mode.

Ebx: This register holds the memory address from where the data needs to be
loaded. The value stored in ebx is treated as the address.

The processor retrieves the value from the memory location whose address is
currently stored in register ebx.

The retrieved value is then loaded into the eax register.


Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

 How can you perform arithmetic operations directly on memory locations using add
and sub?

- Contrary to a common misconception, you cannot directly perform arithmetic


operations on memory locations using just `add` and `sub` instructions in
assembly language. These instructions work with registers, storing the result
back in a register.

- To achieve arithmetic on memory locations, you’ll need a multi-step


approach:

a. Load the value: Use `mov` with an appropriate addressing mode to load the
value from memory into a register.
b. Perform the operation: Use `add` or `sub` with the register and the operand
(immediate value or another register).
c. Store the result (optional): If modifying the memory value is desired, use
`mov` again to store the final value from the register back to the original
memory location.

- This technique allows you to perform calculations on data stored in memory


locations within your assembly code. Remember, `add` and `sub` directly
modify registers, not memory locations themselves.

5. Conditional Operations:

 How can you use add or sub instructions to perform conditional arithmetic (e.g.,
incrementing or decrementing a value based on a condition)?

- `add` and `sub` alone can’t do conditional arithmetic. You combine them with:
-
1. Comparison instructions (e.g., `cmp`): Set flags based on conditions (zero,
carry, negative).
2. Conditional branching (e.g., `je`, `jg`): Jump based on flag state (condition).
3. `add` or `sub` in specific code sections: Perform arithmetic based on the
jump (e.g., increment if positive).

 Describe a scenario where you might use add and sub to manipulate a counter variable.

Scenario: Keeping track of lives remaining in a game


Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

a. Initialize counter: You can use `mov` to initialize a register (e.g., `ecx`) with the initial
number of lives (e.g., `mov ecx, 3`). This register acts as your counter variable.
b. Life lost: When a life is lost, use `sub` to decrement the counter. For example, `sub
ecx, 1` subtracts 1 from the value in `ecx`.
c. Check for lives remaining: Use comparison instructions (e.g., `cmp ecx, 0`) to
compare the counter with zero.
d. Conditional branching: Based on the comparison result (lives remaining or not), use
branching instructions (e.g., `je game_over`) to jump to different sections of code. For
example, `je game_over` jumps if the counter (`ecx`) becomes zero (no lives left).
e. Continue game: If lives remain (`jne` - jump not equal), the game can continue.

In this scenario, `add` wouldn’t be typically used for the counter itself, but `sub` is used
to decrement the counter variable (`ecx`) each time a life is lost. This demonstrates how
`add` and `sub` can be used for manipulating counter variables in conjunction with other
instructions for comparison and branching.

ACTIVITY NUMBER 3

Prepare and Use the EMU 8086 and insert the CODE

; this example shows the use of aaa instruction (ascii adjust after addition).
; it is used to add huge bcd numbers.

name "bcd_aaa"

org 100h

; first number '9':


mov ah, 09h

; second number '5':


mov al, 05h

; al = al + ah =
; = 09h + 05h = 0eh
add al, ah

; clear tens byte of bcd


; result:
xor ah, ah

; adjust result to bcd form,


; ah = 1, al = 4 -> '14'
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

aaa

; print the result:

; store contents of
; ax register:
mov dx, ax

; print first digit:


mov ah, 0eh
; convert to ascii:
or dh, 30h
mov al, dh
int 10h

; print second digit:


; convert to ascii:
or dl, 30h
mov al, dl
int 10h

; wait for any key press:


mov ah, 0
int 16h

ret ; return control to operating system.

Data Results:

QUESTIONS

1. Describe the Purpose of the aaa Instruction in BCD Addition


 What is the purpose of the aaa instruction in BCD (Binary Coded Decimal)
addition?

In the code snippet, the `aaa` instruction is used to fix the result of adding two
BCD (Binary Coded Decimal) numbers. Here’s why it’s necessary:

a. BCD uses 4 bits to represent each decimal digit (0-9).


Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

b. When adding two BCD digits, the sum might be greater than 9, causing an
overflow within a single byte.
`aaa` jumps in after the addition to ensure a valid two-digit BCD result:
c. If the sum is less than or equal to 9, `aaa` does nothing.
d. If the sum overflows (bigger than 9), `aaa` adjusts it by adding 6 (0110 in
binary) to the lower 4 bits and setting the carry flag. This effectively creates two
separate decimal digits: one in the lower nibble and one carried over.

In simpler terms, `aaa` acts like a helper that fixes your finger addition when you
end up with more than 9 fingers! It borrows a “digit” (adds 6) and remembers to
carry over the extra one.

 Explain how the aaa instruction adjusts the result of adding two BCD numbers

- The `aaa` (ASCII Adjust after Addition) instruction in assembly language


adjusts the result of adding two BCD numbers to ensure the result is a valid
BCD value. In the provided code, `ah` is loaded with `09h` and `al` with
`05h`. After adding these (`add al, ah`), `al` contains `0Eh` (14 in decimal).
The `aaa` instruction then checks if the lower nibble of `al` is greater than `9`
or if the Auxiliary Carry flag is set. Since `0Eh` is not a valid BCD, `aaa` adds
`6` to `al` and increments `ah` by `1`, adjusting the result to `14` in BCD form
(`ah` = `1` and `al` = `4`). This adjustment ensures that the sum of the two
BCD numbers is properly formatted as BCD.

 In the context of the provided code, why is it necessary to clear the high Byte (ah)
before using the aaa instruction?

- Clearing the high byte (`ah`) before using the `aaa` instruction is necessary in
the provided code to ensure accurate BCD adjustment. By resetting `ah` to
zero using `xor ah, ah`, any previous data in `ah` is eliminated, preventing
interference with the adjustment process. This step guarantees that `aaa`
correctly handles any carry generated during the BCD adjustment, ensuring
the accuracy of the final result.

2. Understanding ASCII Conversion and Display


 How does the assembly code convert the BCD result (14) into ASCII characters
for display?
 Explain the significance of using or dh, 30h and or dl, 30h before converting and
printing the BCD digits.
 Which BIOS interrupt (int 10h) function is used to display characters on the
screen in this code?
3. Discussing Input and Output Handling
 Why does the code wait for a key press (int 16h) at the end?
Name: Kathlene kaye S. Cleto Date: May 17, 2024
Year/Course/Section: BSEE-3C-M Instructor: Edmund Monilar

 What would happen if the wait for a key press (int 16h) was omitted from the
code?
 How would you modify this code to handle input of the BCD numbers from the
user before performing the addition and displaying the result?

You might also like