Coal Lab Assignment1
Coal Lab Assignment1
ID : F2022266476
COAL ASSIGNMENT 2
Q NO 1:
.model small
.stack 100h
.code
main proc
mov cx,11
go:
mov ah,2
mov dx,cx
add dx,'0'
int 21h
loop go
mov ah,4ch
int 21h
QNO 2:
.model small
.stack 100h
.data
num1 dw 5
num2 dw 10
result dw ?
msg db 'Result: $'
newline db 0dh, 0ah, '$'
.code
main proc
mov ax, @data ; initialize data segment
mov ds, ax
mov ax, num1
mov bx, num2
mul bx
mov result, ax
lea dx, msg ; load address of the message
mov ah, 09h ; DOS function 9h (print string)
int 21h
Q NO 3,4,5:
Adding two numbers stored in registers:
mov ax, 10
mov bx, 5
sub ax, bx
Q NO 6:
Mov ax,bx
This command will mov the data store in bx to ax.
MOV is used to copy data from one location to another. It transfers the contents of the source
operand to the destination operand.
ADD is used for addition operations. It adds the value of the source operand to the value of
the destination operand and stores the result in the destination operand.
QNO 7:
1. ADD (Addition) Instruction:
The ADD instruction performs a simple addition operation between two operands: the
destination operand and the source operand.
It simply adds the source operand to the destination operand and stores the result in the
destination operand.
Example: `ADD destination, source`
2. ADC (Add with Carry) Instruction:
The ADC instruction also performs addition but includes the carry flag (CF) as an additional
input. It adds the source operand, along with the value of the carry flag (CF), to the destination
operand.
This instruction is commonly used in multi-byte addition operations, where a carry
occurs from a lower-order byte to a higher-order byte.
Example: ADC destination, source
EXAMPLE :
QNO 8:
The SBB (subtract with borrow) instruction in assembly language is used to subtract the value
of the source operand from the value of the destination operand, along with the value of the
carry flag (CF), and store the result in the destination operand. It serves the purpose of
performing subtraction operations while considering a borrow from a previous operation.
The syntax for the SBB instruction generally follows this pattern
SBB destination, source
Q NO 9:
Compatibility with ASCII: ASCII characters are commonly represented in hexadecimal. When
dealing with character data, using hexadecimal allows for direct mapping to ASCII values,
simplifying string manipulation, input/output operations, and character-based processing.
Q NO 10:
READING A CHARACTER FROM KEYBOARD:
We can read a character input using this syntax:
Mov ah,1
Int 21h
The entered character will store in al.
Q NO 11:
.model small
.stack 100h
.data
newline db 0Dh, 0Ah, '$' ; newline characters for DOS
.code
main proc
mov ax, @data
mov ds, ax
; Prompt for input
lea dx, prompt
mov ah, 09h
int 21h
read_char:
mov ah, 01h
int 21h
cmp al, 0Dh
je done
mov dl, al
mov ah, 02h
int 21h
jmp read_char
done:
; Print newline
lea dx, newline
mov ah, 09h
int 21h
mov ah, 4Ch
int 21h
main endp
prompt a string: $'
end main
Q NO 12:
.model small
.stack 100h
.code
Main proc
Mov ah,1
Int 21h
Mov ah,4ch
Int 21h
Q NO 13:
.model small
.stack 100h
.code
Msg db “HELLO WORLD:$”
.data
Mov ax,@data
Mov ds,ax
Mov ah,9
Lea dx,Msg
Int 21h
Mov ah,4ch
Int 21h
QNO 14:
The ADC (Add with Carry) instruction is commonly used in scenarios where arithmetic
operations involve multiple bytes and carry propagation is necessary. Here are some scenarios
where ADC is frequently employed:
1. Multi-byte Addition:
ADC is extensively used in multi-byte addition operations where carrying over from lower-
order bytes to higher-order bytes is required.
For example, when adding two multi-byte integers or performing arithmetic operations on
multi-byte data types (such as integers larger than the CPU's word size), ADC ensures correct
handling of carry between bytes.Q NO 15:
Both the Carry Flag (CF) and Overflow Flag (OF) are crucial in relation to the SBB instruction:
CF serves as the borrow flag, indicating whether a borrow occurred during the subtraction.
OF indicates whether an overflow occurred during the subtraction, highlighting situations
where the result exceeds the representable range in signed arithmetic.
Q NO 16:
To print a string using the provided assembly code:
1. Define the string you want to print in the `.data` section.
2. Load the address of the string into the `DX` register using the `LEA` instruction.
3. Set the `AH` register to `9`, indicating the DOS interrupt service for printing a string.
4. Invoke interrupt 21h (`int 21h`), which triggers DOS to print the null-terminated string
located at the memory address specified in `DX`.
5. Terminate the program using a DOS exit service.
Q NO 17 :
Q NO 18:
In computer architecture, particularly in x86 assembly language programming, the AH register
plays a crucial role in printing strings when working with DOS or BIOS interrupts. The AH
register, or "accumulator high," is one of the general-purpose registers available in x86 CPUs,
and its usage varies depending on the context of the program.
When printing strings using DOS or BIOS interrupts in x86 assembly language, such as INT 21h
(DOS interrupt) or INT 10h (BIOS interrupt), the AH register typically holds the function code
that specifies the action to be performed. Specifically, when printing strings, the AH register
often holds the value that indicates the desired operation, such as displaying characters,
reading input, or controlling the display.
Q NO 19:
To print the string on a new line, you typically need to print a carriage return (CR) and line
feed (LF) characters (\r\n) before printing the string itself.When the INT 21h interrupt is
invoked, DOS will print the string to the console along with the newline characters, resulting in
the string being printed on a new line.
Q NO 20:
Q NO 21 :
Line feed (`LF`) and carriage return (`CR`) characters are control characters that play a crucial
role in text output, especially in contexts where text formatting and display are important,
such as in programming, printing, and communication protocols. Here's the significance of
these characters:
Q NO 22:
In assembly language programming, several number systems are commonly used, each with its
specific purposes and representations. The most common number systems include:
Q NO 23:
Certainly! Here are examples of representing the number 42 in binary, decimal, and
hexadecimal:
Binary Representation: `101010b`
Decimal Representation: `42`
Hexadecimal Representation: `2Ah`
These representations illustrate how the same number, 42, can be expressed differently in
binary, decimal, and hexadecimal number systems.
Q NO 24 :
Convenient Addressing: Memory addresses in many systems are commonly represented in
hexadecimal. Using hexadecimal in assembly programming enables direct and intuitive
representation of memory addresses, making it easier to work with pointers, arrays, and
memory manipulation operations.
Compatibility with ASCII: ASCII characters are commonly represented in hexadecimal. When
dealing with character data, using hexadecimal allows for direct mapping to ASCII values,
simplifying string manipulation, input/output operations, and character-based processing.
Q NO 25:
for adding and subtracting binary numbers in assembly language:
Q NO 26:
Q NO 27:
Carrying and borrowing are fundamental concepts in binary arithmetic, playing a crucial role in
performing addition and subtraction operations. Here's why carrying and borrowing are
important:
Carrying in Binary Addition:
In binary addition, carrying occurs when the sum of two digits exceeds the base (which is 2 in
binary). For example, when adding 1 + 1 in binary, the result is 10, where 1 is written in the
current position, and a carry of 1 is propagated to the next higher position.
Carrying ensures that the addition operation is performed correctly, preventing overflow and
allowing the correct representation of multi-digit numbers.
Without carrying, the addition result would be incorrect, leading to errors in calculations and
data processing.
Borrowing in Binary Subtraction:
In binary subtraction, borrowing occurs when the minuend (number being subtracted from) is
smaller than the subtrahend (number being subtracted). Borrowing is necessary to perform
the subtraction operation correctly.
Borrowing ensures that the subtraction operation is performed correctly, preventing
underflow and allowing the correct representation of negative numbers.
Without borrowing, the subtraction result would be incorrect, leading to errors in calculations
and data processing.
Q NO 28:
Hexadecimal numbers hold significant importance in assembly language for several reasons:
Memory Addressing : Memory addresses in many systems are commonly represented in
hexadecimal. Using hexadecimal in assembly language enables direct and intuitive
representation of memory addresses, making it easier to work with pointers, arrays, and
memory manipulation operations. It also facilitates compatibility with memory-related
instructions and operations.Character Representation: Hexadecimal digits directly map to
ASCII characters, with the digits 0-9 corresponding to their ASCII values and the letters A-F
representing decimal values 10-15. This property allows hexadecimal numbers to be used for
character representation and manipulation, facilitating string operations, input/output
operations, and character-based processing.
Q NO 29 :
MOV AX, 0x0A ; Load the hexadecimal value 0x0A into the AX register
ADD AX, 0x0F ; Add the hexadecimal value 0x0F to the AX register
Q NO 30:
Hexadecimal numbers in assembly language are represented using digits 0-9 and letters A-F,
prefixed with `0x`. They are loaded into registers or memory using instructions like `MOV`,
manipulated with arithmetic operations such as `ADD` and `SUB`, compared using `CMP`, and
used for memory access via instructions like `LOAD` and `STORE`.
Q NO 31:
When adding hexadecimal numbers the carry might occur during the addition. Let's add
`7FFFFFFFh` and `80000000h`:
7FFFFFFFh
+ 80000000h
__________
FFFFFFFFh
Adding `7FFFFFFFh` and `80000000h` results in `FFFFFFFFh`. However, note that this result is
one bit longer than the original 32 bits, indicating an overflow. In 32-bit arithmetic, the carry
bit would be lost, resulting in a wraparound to negative values.
Q NO 32:
To subtract hexadecimal numbers, we need to be mindful of borrowing that might occur
during the subtraction. Let's subtract `200h` from `300h`:
300h
- 200h
______
100h
Subtracting `200h` from `300h` results in `100h`. No borrow is necessary in this case because
the subtraction can be performed without borrowing from higher positions.