Assembly
Assembly
Department of
1.
a) ADD AX, BX
Flags:
• CF: 0 (no carry out of the most significant bit)
• SF: 1 (most significant bit is 1, indicating a negative result)
• ZF: 0 (result is not zero)
• PF: 1 (number of 1 bits in the low byte of the result is even)
• OF: 1 (addition of two positive numbers resulted in a negative result)
b) SUB AL, BL
Flags:
• CF: 1 (borrow occurred)
• SF: 0 (most significant bit is 0, indicating a positive result)
• ZF: 0 (result is not zero)
• PF: 0 (number of 1 bits in the result is odd)
• OF: 0 (no overflow)
c) DEC AL
Flags:
• CF: 0 (DEC instruction does not affect CF)
• SF: 1 (most significant bit is 1, indicating a negative result)
• ZF: 0 (result is not zero)
• PF: 1 (number of 1 bits in the result is even)
• OF: 0 (no overflow)
d) NEG AL
e) XCHG AX, BX
f) ADD AL, BL
Flags:
• CF: 1 (carry out of the most significant bit)
• SF: 0 (most significant bit is 0, indicating a positive result)
• ZF: 0 (result is not zero)
• PF: 0 (number of 1 bits in the result is odd)
• OF: 1 (addition of a positive and a negative number resulted in overflow)
g) SUB AX, BX
Initial values: AX = 0000h, BX = 8000h
h) NEG AX
Flags:
• CF: 1 (result is non-zero)
• SF: 1 (most significant bit is 1, indicating a negative result)
• ZF: 0 (result is not zero)
• PF: 1 (number of 1 bits in the low byte of the result is even)
• OF: 0 (no overflow)
2.
a) Condition: AX and BX are positive.
Result: Carry into the MSB but no carry out of the MSB indicates signed overflow.
There is a carry into the MSB, but no carry out, indicating signed overflow because
the result is negative (BFFFh).
Result: Carry out of the MSB but no carry into the MSB indicates signed overflow.
There is no carry into the MSB, but there is a carry out, indicating signed overflow
because the result is positive (0000h)
3.
a)
Flags:
• Signed Overflow: Yes (carry into the MSB without carry out)
• Unsigned Overflow: No (no carry out)
b)
Flags:
c)
Flags:
Flags:
• Signed Overflow: Yes (carry into the MSB without carry out)
• Unsigned Overflow: No (no carry out)
e)
Flags:
4.
a)
AX = 2143h = 0010 0001 0100 0011
BX = 1986h = 0001 1001 1000 0110
SUB = 07BDh = 0000 1011 0111 1101
Flags:
• Signed Overflow: No (no overflow condition met)
• Unsigned Overflow: No (no borrow)
b)
AX = 81FEh = 1000 0001 1111 1110
BX = 1986h = 0001 1001 1000 0110
SUB = 6878h = 0110 0111 0111 1000
Flags:
• Signed Overflow: No (no overflow condition met)
• Unsigned Overflow: Yes (borrow)
c)
AX = 193Ch = 0001 1001 0011 1100
BX = 81FEh = 1000 0001 1111 1110
SUB = 973Eh = 1001 0111 0011 1110
Flags:
• Signed Overflow: Yes (borrow with negative result)
• Unsigned Overflow: Yes (borrow)
d)
AX = 0002h = 0000 0000 0000 0010
BX = FE0Fh = 1111 1110 0000 1111
SUB = 01F3h = 0000 0001 1111 0011
Flags:
• Signed Overflow: No (no overflow condition met)
• Unsigned Overflow: Yes (borrow)
e)
AX = 8BCD = 1000 1011 1100 1101
BX = 71AB = 0111 0001 1010 1011
SUB = 1A22h = 0001 1010 0010 0010
Flags:
• Signed Overflow: Yes (borrow with negative result)
• Unsigned Overflow: No (no borrow)
Chapter: Six
1.
LESS_THAN_ZERO:
; AX < 0: put -1 in BX
MOV BX, -1
CONTINUE:
; Continue with the rest of the code
assembly code
; Check if AL < 0
CMP AL, 0
JL AL_LESS_THAN_ZERO
; AL >= 0: put 0 in AH
MOV AH, 0
JMP CONTINUE_B
AL_LESS_THAN_ZERO:
; AL < 0: put FFh in AH
MOV AH, 0FFh
CONTINUE_B:
; Continue with the rest of the code
c) Suppose DL contains the ASCII code of a character. IF DL >= 'A' AND DL <=
'Z' THEN display DL END_IF
assembly code
; Check if DL >= 'A'
CMP DL, 'A'
JL NOT_IN_RANGE
NOT_IN_RANGE:
; Continue with the rest of the code
ELSE_BLOCK:
; BX >= CX: put 0 in BX
MOV BX, 0
CONTINUE_D:
; Continue with the rest of the code
e) IF (AX < BX) OR (BX < CX) THEN put 0 in DX ELSE put 1 in DX END_IF
assembly code
; Check if AX < BX
CMP AX, BX
JL OR_TRUE
; Check if BX < CX
CMP BX, CX
JL OR_TRUE
OR_TRUE:
; One of the conditions is true: put 0 in DX
MOV DX, 0
CONTINUE_E:
; Continue with the rest of the code
; Check if BX < CX
CMP BX, CX
JL OR_TRUE
; Both conditions are false: put 1 in DX
MOV DX, 1
JMP CONTINUE_E
OR_TRUE:
; One of the conditions is true: put 0 in DX
MOV DX, 0
CONTINUE_E:
; Continue with the rest of the code
2.
Read a character. If it's 'A', execute a carriage return. If it's 'B', execute a line feed.
If it's any other character, return to DOS.
assembly code
; Read a character into AL
MOV AH, 1
INT 21h
CARRIAGE_RETURN:
; Execute carriage return
MOV AH, 2
MOV DL, 0Dh
INT 21h
JMP EXIT
LINE_FEED:
; Execute line feed
MOV AH, 2
MOV DL, 0Ah
INT 21h
EXIT:
; Exit the program
INT 20h
3.
SUM_LOOP:
ADD AX, CX ; Add the current term to AX
ADD CX, DX ; Increment the term by the common
difference
CMP CX, 148 ; Check if the term has reached 148
JLE SUM_LOOP ; Loop if it has not
SUM_LOOP2:
ADD AX, CX ; Add the current term to AX
SUB CX, DX ; Decrement the term by the common
difference
CMP CX, 5 ; Check if the term has reached 5
JGE SUM_LOOP2 ; Loop if it has not
4.
a) Put the sum of the first 50 terms of the arithmetic sequence 1, 5, 9, 13, ... in DX.
Assembly code
MOV CX, 50 ; Number of terms
MOV AX, 1 ; First term
MOV BX, 4 ; Common difference
MOV DX, 0 ; Initialize sum in DX
SUM_LOOP3:
ADD DX, AX ; Add the current term to the sum
ADD AX, BX ; Increment the term by the common
difference
LOOP SUM_LOOP3 ; Loop until CX is 0
DISPLAY_LOOP:
MOV AH, 2
INT 21h ; Display the character in DL
LOOP DISPLAY_LOOP
; New line
MOV AH, 2
MOV DL, 0Ah
INT 21h
READ_LOOP:
MOV AH, 1
INT 21h ; Read a character
LOOP READ_LOOP
; Carriage return
MOV AH, 2
MOV DL, 0Dh
INT 21h
DISPLAY_X:
MOV DL, 'X'
INT 21h
LOOP DISPLAY_X
DIV_LOOP:
CMP AX, BX ; Compare dividend and divisor
JB END_DIV ; If AX < BX, division is done
END_DIV:
; CX now contains the quotient
MULT_LOOP:
CMP BX, 0 ; Check if BX is 0
JE END_MULT ; If BX is 0, multiplication is done
END_MULT:
; CX now contains the product
7.
READ_LOOP:
MOV AH, 1
INT 21h ; Read a character into AL
CMP AL, ' ' ; Compare with blank character
LOOPE READ_LOOP ; Loop if AL is blank and CX is not 0
READ_LOOP2:
MOV AH, 1
INT 21h ; Read a character into AL
CMP AL, 0Dh ; Compare with carriage return
LOOPNE READ_LOOP2 ; Loop if AL is not carriage return and
CX is not 0
Chapter: Seven
1.
a)
10101111 AND 10001011
10101111
10001011
10001011
b)
10110001 OR 01001001
10110001
01001001
11111001
c)
01111100 XOR 11011010
01111100
11011010
10100110
d)
NOT 01011110
01011110
10100001
2.
a) Clear the even-numbered bits of AX, leaving the other bits unchanged
b) Set the most and least significant bits of BL, leaving the other bits unchanged
OR BL, 0x81
d) Replace the value of the word variable WORD1 by its one's complement
NOT WORD1
3.
a) Set ZF if the contents of AX is zero
TEST AX, AX
(Note: This is the same as part c because the result will be zero if DX is non-negative.)
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 1|11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 11100101|1
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 11100101, 𝐶𝐹 = 1
b) SHR AL, 1
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑆ℎ𝑖𝑓𝑡: 01100101
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 01100101, 𝐶𝐹 = 1
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 001011|11
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 00101111, 𝐶𝐹 = 1
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 011|11001
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 10111001, 𝐶𝐹 = 1
f) RCL AL, 1
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 1|10010110
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 10010110, 𝐶𝐹 = 1
𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 011|11001
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 01110011, 𝐶𝐹 = 1
5.
To double a value in assembly language, you can use the SHL (Shift Left) instruction.
SHL BS, 1
Before: 00001101
Shift Left: 00011010
After: 00011010 (26 in decimal)
SHL AL, 3
Before: 00000011
Shift Left by 3: 00011000
After: 00011000 (24 in decimal)
c) Divide 32142 by 4 and put the quotient in AX.
6.
5 + 0x30 = 35 (0x23)
Result: 00110011 (which is '5' in ASCII)
OR DL, 20h
Binary Calculation: If DL = 01000001 ('A' in ASCII),
7.
MOV AX, BL
MOV CX, 10
MUL CL
MOV BL, AL
3 * 10 = 30 (00011110 in binary)
To get the remainder when dividing by 8, use the ROR (Rotate Right) instruction to get the
last 3 bits.
MOV AH, AL
AND AH, 07h
SHR AL, 3
AL / 8 = 3 (00000011)
Remainder = 1 (00000001)
Chapter: Eight
1.
The stack segment is declared as .STACK 100h. When the program begins, SP (Stack
Pointer) points to the top of the stack, which is 100h.
b) What is the maximum hex number of words that the stack may contain?
The maximum number of words the stack can contain is 100h / 2 = 80h words, since
each word is 2 bytes.
2.
Given:
• AX = 1234h
• BX = 5678h
• CX = 9ABCh
• SP = 100h
Instructions:
Assembly code
PUSH AX
PUSH BX
XCHG AX, CX
POP CX
PUSH AX
POP BX
Step-by-step Execution:
1. PUSH AX:
• SP = SP - 2 = 100h - 2 = 0FEh
• [0FEh] = 1234h
2. PUSH BX:
• SP = 0FEh - 2 = 0FCh
• [0FCh] = 5678h
• AX = 9ABCh
• CX = 1234h
4. POP CX:
• CX = [0FCh] = 5678h
• SP = 0FCh + 2 = 0FEh
5. PUSH AX:
• SP = 0FEh - 2 = 0FCh
• [0FCh] = 9ABCh
6. POP BX:
• BX = [0FCh] = 9ABCh
• SP = 0FCh + 2 = 0FEh
Final Contents:
• AX = 9ABCh
• BX = 9ABCh
• CX = 5678h
• SP = 0FEh
3. When SP = 0 and another word is pushed onto the stack, it will cause a stack overflow, which may
overwrite other data or code segments, leading to unpredictable behavior or program crashes.
4. Given:
5. Given:
• SP = 0200h
• Top of stack = 012Ah
• IP = 012Ah
• SP = 0200h + 2 = 0202h
• IP = 012Ah
• SP = 0200h + 2 + 4 = 0206h
6.
a. Place the top of the stack into AX, without changing stack contents.
Assembly code
MOV AX, [SP]
b. Place the word below the stack top into CX, without changing stack contents. You
may use AX.
Assembly code
MOV AX, SP
ADD AX, 2
MOV CX, [AX]
c. Exchange the top two words on the stack. You may use AX and BX.
Assembly code
POP AX
POP BX
PUSH AX
PUSH BX
7.
1.
Binary Calculation:
• AX: 0018h
• DX: 0000h
• CF: 0
• OF: 0
MUL BX
Binary Calculation:
• AX: 0000h
• DX: 0FF0h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)
IMUL CX
Binary Calculation:
• AX: FFFBh
• DX: FFFFh (sign extension of the result)
• CF: 0
• OF: 0
IMUL WORD1
Binary Calculation:
• AX: 0000h
• DX: 8000h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)
MUL 10h
Binary Calculation:
• AX: 0000h
• DX: 0FF0h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)
2.
Binary Calculation:
• AX: 0A0Bh
• CF: 1 (because AH is non-zero)
• OF: 1 (because AH is non-zero)
IMUL BL
Binary Calculation:
MUL AH
Binary Calculation:
• AX: 01A8h
• CF: 0
• OF: 0
d) IMUL BYTE1, if AL contains 02h and BYTE1 contains FBh.
IMUL BYTE1
Binary Calculation:
• AX: FFF6h
• CF: 0
• OF: 0
3.
Binary Calculation:
• AX: 0003h
• DX: 0001h
• No overflow
DIV BX
Binary Calculation:
• AX: 0FFEh
• DX: 000Eh
• No overflow
c) IDIV BX, if DX contains FFFFh, AX contains FFFC, and BX contains 0003h.
IDIV BX
Binary Calculation:
DX:AX = FFFF FFFCh (1111 1111 1111 1111 1111 1100) -> -4 in signed
BX = 0003h (0000 0000 0000 0011)
Result: (DX:AX) / BX = -4 / 3 = -1 remainder -1
• AX: FFFDh
• DX: FFFDh
• No overflow
DIV BX
Binary Calculation:
4.
Binary Calculation:
• AL: 04h
• AH: 01h
• No overflow
b) IDIV BL, if AX contains FFFBh and BL contains FEh.
IDIV BL
Binary Calculation:
• AL: 02h
• AH: FFh
• No overflow
DIV BL
Binary Calculation:
• AL: 0Fh
• AH: 0Eh
• No overflow
•
DIV BL
Binary Calculation:
• AL: 07Fh
• AH: 00h
• No overflow
5.
a) 7E02h
CWD
b) 8ABC
CWD
c. 1ABC
CWD
6.
a) F0h
CBW
Binary Calculation:
• AX: FFF0h
b. 5Fh
CBW
Binary Calculation:
c. 80h
CBW
Binary Calculation:
• AX: FF80h
7.
a) A = 5 * A - 7
assembly code
; Assume A is in AX
IMUL AX, AX, 5 ; AX = 5 * A
SUB AX, 7 ; AX = AX - 7 (A = 5 * A - 7)
MOV [A], AX ; Store the result back to A
b) B = (A - B) * (B + 10)
assembly code
; Assume A is in AX and B is in BX
MOV CX, AX ; CX = A
SUB CX, BX ; CX = A - B
MOV DX, BX ; DX = B
ADD DX, 10 ; DX = B + 10
IMUL CX, DX ; CX = (A - B) * (B + 10)
MOV [B], CX ; Store the result back to B
c) A = 6 - 9 * A
assembly code
; Assume A is in AX
IMUL AX, AX, 9 ; AX = 9 * A
MOV BX, 6 ; BX = 6
SUB BX, AX ; BX = 6 - (9 * A)
MOV AX, BX ; AX = BX
MOV [A], AX ; Store the result back to A
d) IF A2 + B2 = C2 THEN set CF ELSE clear CF
assembly code