0% found this document useful (0 votes)
13 views

Assembly

Solution problems of assembly language

Uploaded by

alamin.sust57
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assembly

Solution problems of assembly language

Uploaded by

alamin.sust57
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Shahjalal University of Science and Technology, Sylhet

Department of

Computer Science & Engineering

Course Name : Microprocessor and Interfacing

Course Code : CSE-367

Assignment Name : Assembly Language Programming

Submitted By: Submitted To:

Al-Amin Abdullah Al Noman

Reg. no.: 2020331057 Lecturer,

Session: 2020-21 Department of Computer Science &


Engineering, SUST
Email: [email protected]
Department of Computer Science &
Engineering, SUST
Chapter: Five

1.
a) ADD AX, BX

Initial values: AX = 7FFFh, BX = 0001h

Operation: AX = AX + BX = 7FFFh + 0001h = 8000h


= 1000000000000000

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

Initial values: AL = 01h, BL = FFh

Operation: AL = AL - BL = 01h - FFh = 01h + (01h complement of


FFh) = 01h + 01h = 02h = 00000010

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

Initial values: AL = 00h

Operation: AL = AL - 1 = 00h - 01h = FFh = 11111111

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

Initial values: AL = 7Fh

Operation: AL = -AL = -7Fh = 81h = 10000001 (in two's complement)


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 result is even)
• OF: 0 (no overflow)

e) XCHG AX, BX

Initial values: AX = 1ABCh, BX = 712Ah

Operation: Exchange the contents of AX and BX: AX = 712Ah, BX = 1ABCh


Flags:
• No flags are affected by the XCHG instruction

f) ADD AL, BL

Initial values: AL = 80h, BL = FFh

Operation: AL = AL + BL = 80h + FFh = 7Fh (addition in two's complement)


= 01111111

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

Operation: AX = AX - BX = 0000h - 8000h = 8000h (subtraction in two's


complement) = 1000000000000000
Flags:
• CF: 1 (borrow occurred)
• 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 (subtraction of a smaller positive number from a larger negative number
resulted in overflow)

h) NEG AX

Initial values: AX = 0001h


Operation: AX = -AX = -0001h = FFFFh (in two's complement)

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.

Operation: ADD AX, BX

Result: Carry into the MSB but no carry out of the MSB indicates signed overflow.

To show this, let's consider:

AX = 0111 1111 1111 1111 (7FFFh)

BX = 0100 0000 0000 0001 (4000h)

Sum = 1011 1111 1111 1111 (BFFFh)

There is a carry into the MSB, but no carry out, indicating signed overflow because
the result is negative (BFFFh).

b) Condition: AX and BX are negative.

Operation: ADD AX, BX

Result: Carry out of the MSB but no carry into the MSB indicates signed overflow.

To show this, let's consider:

AX = 1000 0000 0000 0000 (8000h)

BX = 1000 0000 0000 0000 (8000h)

Sum = 0000 0000 0000 0000 (0000h)

There is no carry into the MSB, but there is a carry out, indicating signed overflow
because the result is positive (0000h)
3.
a)

AX = 512Ch = 0101 0001 0010 1100

BX = 4185h = 0100 0001 1000 0101

Sum = 92B1h = 1001 0010 1011 0001

Flags:

• Signed Overflow: Yes (carry into the MSB without carry out)
• Unsigned Overflow: No (no carry out)

b)

AX = FE12h = 1111 1110 0001 0010

BX = 1ACBh = 0001 1010 1100 1011

Sum = 18EDh = 0001 1000 1110 1101

Flags:

• Signed Overflow: No (no carry into the MSB)


• Unsigned Overflow: Yes (carry out)

c)

AX = E1E4h = 1110 0001 1110 010

BX = DAB3h = 1101 1010 1011 0011

Sum = BC97h = 1011 1100 1001 0111

Flags:

• Signed Overflow: No (no carry into the MSB)


• Unsigned Overflow: Yes (carry out)
d)

AX = 7132h = 0111 0001 0011 0010

BX = 7000h = 0111 0000 0000 0000

Sum = E132h = 1110 0001 0011 0010

Flags:

• Signed Overflow: Yes (carry into the MSB without carry out)
• Unsigned Overflow: No (no carry out)

e)

AX = 6389h = 0110 0011 1000 1001

BX = 1176h = 0001 0001 0111 0110

Sum = 74FFh = 0111 0100 1111 1111

Flags:

• Signed Overflow: No (no carry into the MSB)


• Unsigned Overflow: No (no carry out)

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.

a) IF AX < 0 THEN PUT -1 IN BX END_IF


assembly code
; Check if AX < 0
CMP AX, 0
JL LESS_THAN_ZERO

; Code continues here if AX >= 0


JMP CONTINUE

LESS_THAN_ZERO:
; AX < 0: put -1 in BX
MOV BX, -1

CONTINUE:
; Continue with the rest of the code

b) IF AL < 0 THEN put FFh in AH ELSE put 0 in AH END_IF

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

; Check if DL <= 'Z'


CMP DL, 'Z'
JG NOT_IN_RANGE

; DL is between 'A' and 'Z': display DL


CALL DISPLAY_DL

NOT_IN_RANGE:
; Continue with the rest of the code

d) IF AX < BX THEN IF BX < CX THEN put 0 in AX ELSE put 0 in BX END_IF


END_IF
assembly code
; Check if AX < BX
CMP AX, BX
JNL CONTINUE_D

; AX < BX: check if BX < CX


CMP BX, CX
JNL ELSE_BLOCK

; BX < CX: put 0 in AX


MOV AX, 0
JMP CONTINUE_D

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

; 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

f) IF AX <= BX THEN put 0 in AX ELSE IF BX < CX THEN put 0 in BX ELSE


put 0 in CX END_IF END_IF
assembly code
; Check if AX < BX
CMP AX, BX
JL OR_TRUE

; 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

; Check if the character is 'A'


CMP AL, 'A'
JE CARRIAGE_RETURN

; Check if the character is 'B'


CMP AL, 'B'
JE LINE_FEED

; If it's any other character, return to DOS


INT 20h

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.

a) Put the sum 1 + 4 + 7 + ... + 148 in AX.


Assembly code
MOV AX, 0 ; Initialize AX to 0
MOV CX, 1 ; Start with the first term
MOV DX, 3 ; Common difference

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

; AX now contains the sum

b) Put the sum 100 + 95 + 90 + ... + 5 in AX.


Assembly code
MOV AX, 0 ; Initialize AX to 0
MOV CX, 100 ; Start with the first term
MOV DX, 5 ; Common difference

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

; AX now contains the sum

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

; DX now contains the sum


b) Read a character and display it 80 times on the next line.
Assembly code
; Read a character into AL
MOV AH, 1
INT 21h
; Store the character in DL for display
MOV DL, AL

; Set CX to 80 for 80 repetitions


MOV CX, 80

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

MOV DL, 0Dh


INT 21h

c) Read a five-character password and overprint it by executing a carriage return


and displaying five X's.
assembly code
MOV CX, 5 ; Number of characters in the password

READ_LOOP:
MOV AH, 1
INT 21h ; Read a character
LOOP READ_LOOP

; Carriage return
MOV AH, 2
MOV DL, 0Dh
INT 21h

; Display five X's


MOV CX, 5

DISPLAY_X:
MOV DL, 'X'
INT 21h
LOOP DISPLAY_X

5. Write a sequence of instructions to divide AX by BX, and put the quotient in


CX.
Assembly code
MOV CX, 0 ; Initialize quotient to 0

DIV_LOOP:
CMP AX, BX ; Compare dividend and divisor
JB END_DIV ; If AX < BX, division is done

SUB AX, BX ; Subtract divisor from dividend


INC CX ; Increment quotient
JMP DIV_LOOP ; Repeat

END_DIV:
; CX now contains the quotient

Write a sequence of instructions to multiply AX by BX, and put the product in


6.
CX.
Assembly code
MOV CX, 0 ; Initialize product to 0

MULT_LOOP:
CMP BX, 0 ; Check if BX is 0
JE END_MULT ; If BX is 0, multiplication is done

ADD CX, AX ; Add AX to the product


DEC BX ; Decrement BX
JMP MULT_LOOP ; Repeat

END_MULT:
; CX now contains the product

7.

a) Write instructions to read characters until either a nonblank character is typed,


or 80 characters have been typed. Use LOOPE.
Assembly code
MOV CX, 80 ; Set counter to 80

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

b) Write instructions to read characters until either a carriage return is typed or 80


characters have been typed. Use LOOPNE.
Assembly code
MOV CX, 80 ; Set counter to 80

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

Mask with 01010101 01010101

AND AX, 0x5555

b) Set the most and least significant bits of BL, leaving the other bits unchanged

Mask with 10000001

OR BL, 0x81

c) Complement the msb of DX, leaving the other bits unchanged

Mask with 1000000000000000

XOR DX, 0x8000

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

b) Clear ZF if BX contains an odd number


TEST BX, 0x0001

c) Set SF if DX contains a negative number


TEST DX, 0x8000

d) Set ZF if DX contains a zero or positive number


TEST DX, 0x8000

(Note: This is the same as part c because the result will be zero if DX is non-negative.)

e) Set PF if BL contains an even number of 1 bits


TEST BL, BL

4. Given: AL = 11001011 and CF = 1


a) ROR AL, 1

Rotate AL right through carry flag by 1 bit:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 1|11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 11100101|1
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 11100101, 𝐶𝐹 = 1

b) SHR AL, 1

Shift AL right by 1 bit:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑆ℎ𝑖𝑓𝑡: 01100101
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 01100101, 𝐶𝐹 = 1

c) ROL AL, CL (if CL contains 2)

Rotate AL left through carry flag by 2 bits:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 001011|11
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 00101111, 𝐶𝐹 = 1

d) ROR AL, CL (if CL contains 3)

Rotate AL right through carry flag by 3 bits:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 011|11001
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 10111001, 𝐶𝐹 = 1

e) SAR AL, CL (if CL contains 2)

Arithmetic shift AL right by 2 bits:


𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑆ℎ𝑖𝑓𝑡: 11110010
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 11110010, 𝐶𝐹 = 1

f) RCL AL, 1

Rotate AL left through carry flag by 1 bit:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 1|10010110
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 10010110, 𝐶𝐹 = 1

g) RCR AL, CL (if CL contains 3)

Rotate AL right through carry flag by 3 bits:

𝐼𝑛𝑖𝑡𝑖𝑎𝑙: 11001011
𝑅𝑜𝑡𝑎𝑡𝑒: 011|11001
𝑅𝑒𝑠𝑢𝑙𝑡: 𝐴𝐿 = 01110011, 𝐶𝐹 = 1

5.

a) Double the value of byte variable BS.

To double a value in assembly language, you can use the SHL (Shift Left) instruction.

SHL BS, 1

Binary Calculation: If BS = 00001101 (13 in decimal),

Before: 00001101
Shift Left: 00011010
After: 00011010 (26 in decimal)

b) Multiply the value of AL by 8.

To multiply by 8, you can shift left by 3 positions (since 8 is 23).

SHL AL, 3

Binary Calculation: If AL = 00000011 (3 in decimal),

Before: 00000011
Shift Left by 3: 00011000
After: 00011000 (24 in decimal)
c) Divide 32142 by 4 and put the quotient in AX.

You can use the DIV instruction.

MOV AX, 32142


MOV CX, 4
DIV CX

Binary Calculation: 32142 in binary is 0111110110111110.

32142 / 4 = 8035 (result in AX)


8035 in binary: 0001111101101111

d) Divide -2145 by 16 and put the quotient in BX.

Use the IDIV (signed division) instruction.

MOV AX, -2145


MOV CX, 16
IDIV CX
MOV BX, AX

Binary Calculation: -2145 in binary (2's complement for a 16-bit number):


1111101111100111

-2145 / 16 = -134 (result in BX)


-134 in binary (2's complement): 1111111101111110

6.

a) Assuming AL has a value less than 10, convert it to a decimal character.

To convert a value less than 10 to a character, add 0x30 to it.

ADD AL, 30h

Binary Calculation: If AL = 5 (00000101 in binary),

5 + 0x30 = 35 (0x23)
Result: 00110011 (which is '5' in ASCII)

b) Assuming DL contains the ASCII code of an uppercase letter, convert it to lower


case.

To convert an uppercase letter to a lowercase letter, add 0x20.

OR DL, 20h
Binary Calculation: If DL = 01000001 ('A' in ASCII),

DL OR 00100000 = 01100001 (which is 'a' in ASCII)

7.

a) Multiply the value of BL by 10d. Assume overflow does not occur.

Multiplying by 10 can be done by multiplying by 2 and then by 5.

MOV AX, BL
MOV CX, 10
MUL CL
MOV BL, AL

Binary Calculation: If BL = 3 (00000011 in binary),

3 * 10 = 30 (00011110 in binary)

b) Suppose AL contains a positive number. Divide AL by 8, and put the remainder in


AH. (Hint: use ROR.)

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

Binary Calculation: If AL = 25 (00011001 in binary),

AL / 8 = 3 (00000011)
Remainder = 1 (00000001)
Chapter: Eight

1.

a) What is the hex contents of SP when the program begins?

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

3. XCHG AX, CX:

• 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:

• Instruction MOV AX, BX is at 08FD:0203h.


• PROC1 begins at 08FD:0300h.
• SP = 010Ah.

After CALL PROC1:

• IP (Instruction Pointer) = 0300h (address of PROC1)


• SP = 010Ah - 2 = 0108h
• [0108h] = 0206h (next instruction after CALL, 08FD:0206h)

5. Given:

• SP = 0200h
• Top of stack = 012Ah

a. After RET is executed:

RET pops the return address from the stack.

• IP = 012Ah
• SP = 0200h + 2 = 0202h

b. After RET 4 is executed:

RET 4 pops the return address and increments SP by an additional 4.

• 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.

a. Implement a procedure SAVE_REGS.


Assembly code
SAVE_REGS PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DI
PUSH BP
PUSH DS
PUSH ES
MOV AX, [SP+18h] ; Store return address in AX
PUSH AX ; Put return address on top of the
stack
ADD SP, 2 ; Adjust SP to account for added
return address
RET
SAVE_REGS ENDP

b. Write a procedure RESTORE_REGS that restores the registers.


Assembly code
RESTORE_REGS PROC
POP ES
POP DS
POP BP
POP DI
POP SI
POP DX
POP CX
POP BX
POP AX
RET
RESTORE_REGS ENDP
Chapter: Nine

1.

a) MUL BX, if AX contains 0008h and BX contains 0003h.


MUL BX

Binary Calculation:

AX = 0008h (0000 0000 0000 1000)


BX = 0003h (0000 0000 0000 0011)
Result: AX * BX = 0008 * 0003 = 0018h (0000 0000 0001 1000)

• AX: 0018h
• DX: 0000h
• CF: 0
• OF: 0

b) MUL BX, if AX contains 00FFh and BX contains 1000h.

MUL BX

Binary Calculation:

AX = 00FFh (0000 0000 1111 1111)


BX = 1000h (0001 0000 0000 0000)
Result: AX * BX = 00FF * 1000 = 0FF000h (0000 1111 1111 0000 0000
0000)

• AX: 0000h
• DX: 0FF0h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)

c) IMUL CX, if AX contains 0005h and CX contains FFFFh.

IMUL CX

Binary Calculation:

AX = 0005h (0000 0000 0000 0101)


CX = FFFFh (1111 1111 1111 1111) -> -1 in signed
Result: AX * CX = 0005 * FFFF = FFFBh (1111 1111 1111 1011) -> -5 in
signed

• AX: FFFBh
• DX: FFFFh (sign extension of the result)
• CF: 0
• OF: 0

d) IMUL WORD1, if AX contains 8000h and WORD1 contains FFFFh.

IMUL WORD1

Binary Calculation:

AX = 8000h (1000 0000 0000 0000) -> -32768 in signed

WORD1 = FFFFh (1111 1111 1111 1111) -> -1 in signed


Result: AX * WORD1 = 8000 * FFFF = 8000h (1000 0000 0000 0000)

• AX: 0000h
• DX: 8000h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)

e) MUL 10h, if AX contains FF00h.

MUL 10h

Binary Calculation:

AX = FF00h (1111 1111 0000 0000)


10h = 0010h (0000 0000 0001 0000)
Result: AX * 10h = FF00 * 0010 = FF000h (1111 1111 0000 0000 0000)

• AX: 0000h
• DX: 0FF0h
• CF: 1 (because DX is non-zero)
• OF: 1 (because DX is non-zero)
2.

a) MUL BL, if AL contains ABh and BL contains 10h.


MUL BL

Binary Calculation:

AL = ABh (1010 1011)


BL = 10h (0001 0000)
Result: AL * BL = AB * 10 = 0A0Bh (0000 1010 0000 1011)

• AX: 0A0Bh
• CF: 1 (because AH is non-zero)
• OF: 1 (because AH is non-zero)

b) IMUL BL, if AL contains ABh and BL contains 10h.

IMUL BL

Binary Calculation:

AL = ABh (1010 1011) -> -85 in signed


BL = 10h (0001 0000)
Result: AL * BL = -85 * 16 = -1360 (0FAF0h in signed)

• AX: FA0h (signed result truncated to 16-bit)


• CF: 1
• OF: 1

c. MUL AH, if AX contains 01A8h.

MUL AH

Binary Calculation:

AX = 01A8h (0000 0001 1010 1000)


AH = 01h (0000 0001)
Result: AL * AH = 01A8 * 01 = 01A8h

• AX: 01A8h
• CF: 0
• OF: 0
d) IMUL BYTE1, if AL contains 02h and BYTE1 contains FBh.

IMUL BYTE1

Binary Calculation:

AL = 02h (0000 0010)


BYTE1 = FBh (1111 1011) -> -5 in signed
Result: AL * BYTE1 = 02 * -5 = -10 (0FFF6h in signed)

• AX: FFF6h
• CF: 0
• OF: 0

3.

a) DIV BX, if DX contains 0000h, AX contains 0007h, and BX contains 0002h.


DIV BX

Binary Calculation:

DX:AX = 0000 0007h (0000 0000 0000 0000 0000 0111)


BX = 0002h
Result: (DX:AX) / BX = 7 / 2 = 3 remainder 1

• AX: 0003h
• DX: 0001h
• No overflow

b) DIV BX, if DX contains 0000h, AX contains FFFEh, and BX contains 0010h.

DIV BX

Binary Calculation:

DX:AX = 0000 FFFEh (0000 0000 1111 1111 1111 1110)


BX = 0010h
Result: (DX:AX) / BX = 65534 / 16 = 4095 remainder 14

• 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

d. DIV BX, same values as part c.

DIV BX

Binary Calculation:

• AX = FFFFh (quotient, representing -1)


• DX = FFFFh (remainder, representing -1)
• No overflow occurred during the division.

4.

a) DIV BL, if AX contains 000Dh and BL contains 03h.


DIV BL

Binary Calculation:

AX = 000Dh (0000 0000 0000 1101)


BL = 03h (0000 0011)
Result: AX / BL = 13 / 3 = 4 remainder 1

• AL: 04h
• AH: 01h
• No overflow
b) IDIV BL, if AX contains FFFBh and BL contains FEh.

IDIV BL

Binary Calculation:

AX = FFFBh (1111 1111 1111 1011) -> -5 in signed


BL = FEh (1111 1110) -> -2 in signed
Result: AX / BL = -5 / -2 = 2 remainder -1

• AL: 02h
• AH: FFh
• No overflow

c) DIV BL, if AX contains 00FEh and BL contains 10h.

DIV BL

Binary Calculation:

AX = 00FEh (0000 0000 1111 1110)


BL = 10h (0001 0000)
Result: AX / BL = 254 / 16 = 15 remainder 14

• AL: 0Fh
• AH: 0Eh
• No overflow

d) DIV BL, if AX contains FF0h and BL contains 02h.

DIV BL

Binary Calculation:

AX = 0FF0h (0000 1111 1111 0000)


BL = 02h (0000 0010)
Result: AX / BL = 4080 / 2 = 2040 remainder 0

• AL: 07Fh
• AH: 00h
• No overflow
5.

a) 7E02h
CWD

• AX: 7E02h (0111 1110 0000 0010) -> positive number


• DX: 0000h (CWD sign extends AX)

b) 8ABC

CWD

• AX: 8ABC (1000 1010 1011 1100) -> negative number


• DX: FFFFh (CWD sign extends AX)

c. 1ABC

CWD

• AX: 1ABC (0001 1010 1011 1100) -> positive number


• DX: 0000h (CWD sign extends AX)

6.

a) F0h
CBW

Binary Calculation:

AL = F0h (1111 0000) -> -16 in signed


AX after CBW = FFF0h (1111 1111 1111 0000)

• AX: FFF0h

b. 5Fh

CBW

Binary Calculation:

AL = 5Fh (0101 1111) -> 95 in unsigned (positive number)


AX after CBW = 005Fh (0000 0000 0101 1111)
• AX: 005Fh

c. 80h

CBW

Binary Calculation:

AL = 80h (1000 0000) -> -128 in signed


AX after CBW = FF80h (1111 1111 1000 0000)

• 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

; Assume A is in AX, B is in BX, and C is in CX


IMUL DX, AX, AX ; DX = A^2
IMUL SI, BX, BX ; SI = B^2
ADD DX, SI ; DX = A^2 + B^2
IMUL SI, CX, CX ; SI = C^2
CMP DX, SI ; Compare A^2 + B^2 with C^2
JE SET_CF ; If equal, jump to SET_CF
CLC ; Else, clear CF
JMP DONE ; Jump to DONE
SET_CF:
STC ; Set CF
DONE:

You might also like