Assembly Language Programming
Assignment – Answers
Part A: Conceptual Questions
1. 1. Difference between JMP and conditional jumps:
JMP always jumps, no condition. Conditional jumps (JE, JG, etc.) jump only if certain
conditions are true.
2. 2. How high-level structures are written in assembly:
Use CMP and conditional jumps. Examples:
- if: CMP AX, BX; JE label
- if-else: CMP AX, BX; JE label1; JMP label2
- while: CMP AX, 0; JE end; JMP loop_start
3. 3. Role of CMP:
CMP subtracts one value from another and sets FLAGS like Zero, Carry, and Sign. Used for
decision making.
4. 4. What happens in a loop:
CMP checks condition, loop continues with jump if true, else exits.
5. 5. LOOP vs JCXZ:
LOOP uses CX and jumps while CX ≠ 0. JCXZ jumps only if CX = 0.
6. 6. CMP with conditional jumps:
CMP sets flags. Conditional jumps use those flags to decide whether to jump.
7. 7. Signed vs Unsigned jumps:
Signed: JG, JL. Unsigned: JA, JB. Use based on signedness of data.
8. 8. How 'for' loops work:
Initialize counter, CMP, condition check, increment inside loop, jump to start.
9. 9. Effects of CMP on FLAGS:
Changes Zero, Carry, Sign, Overflow flags, which affect jumps.
10. 10. JCXZ function:
Jumps if CX = 0. Useful to skip loop if CX is initially zero.
11. 11. Four conditional jumps:
JE (equal), JNE (not equal), JG (greater), JL (less)
12. 12. Nested loops and conditionals:
Use multiple labels. Carefully handle jump targets.
13. 13. FLAGS in control flow:
FLAGS determine whether a condition is met for jumping.
14. 14. LOOP with other jumps precautions:
Be cautious with labels and make sure CX is not overwritten accidentally.
15. 15. CMP vs JCXZ:
CMP + JE is flexible. JCXZ is simpler but only works when checking CX at zero.
Part B: Programming Exercises
16. If-Else Simulation:
.MODEL SMALL
.STACK 100H
.DATA
A DW 7
B DW 5
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AX, A
CMP AX, B
JG A_GREATER
JL B_GREATER
MOV AH, 09H
LEA DX, EQ_MSG
INT 21H
JMP END_PROGRAM
A_GREATER:
MOV AH, 09H
LEA DX, A_MSG
INT 21H
JMP END_PROGRAM
B_GREATER:
MOV AH, 09H
LEA DX, B_MSG
INT 21H
END_PROGRAM:
MOV AH, 4CH
INT 21H
MAIN ENDP
.DATA
A_MSG DB 'A is greater$'
B_MSG DB 'B is greater$'
EQ_MSG DB 'A equals B$'
END MAIN
17. Counting Loop (1 to 10):
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 'Number: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV CX, 10
MOV BL, 1
PRINT_LOOP:
MOV AH, 09H
LEA DX, MSG
INT 21H
MOV AL, BL
ADD AL, 30H
MOV DL, AL
MOV AH, 02H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
MOV DL, 0DH
INT 21H
INC BL
LOOP PRINT_LOOP
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN