Assembly code comments
Below is your required format:
✔ Title highlighted
✔ Comments added line-by-line in the same style you showed
✔ Major comments with “; ---- Step X: … ----”
✔ Algorithm included
✔ Formatting clean and arranged
🌟 Program: Display Two Messages Using DOS Interrupt 21H
📌 Algorithm
1. Start the program
2. Define the stack, data, and code segments
3. Store two messages: "HELLO" and "WORLD" ending with $ for DOS function 09H
4. Initialize DS (Data Segment register) with the address of the data segment
5. Load the address of MSG1 into DX
6. Set AH = 09H to select DOS print-string function
7. Call INT 21H to print "HELLO"
8. Load the address of MSG2 into DX
9. Again set AH = 09H
10. Call INT 21H to print "WORLD"
11. Set AH = 4CH to exit program
12. Call INT 21H
13. Stop program
✨ ASSEMBLY LANGUAGE PROGRAM WITH COMMENTS
asm
; ============================================
; PROGRAM TO PRINT TWO MESSAGES
; ============================================
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'HELLO$' ; Message 1: '$' marks end for DOS 09H
MSG2 DB 'WORLD$' ; Message 2
.CODE
MAIN PROC
; ---- Step 1: Initialize Data Segment ----
MOV AX, @DATA ; Load base address of DATA segment into AX
MOV DS, AX ; Move it into DS register for accessing variables
; ---- Step 2: Print First Message "HELLO" ----
MOV DX, OFFSET MSG1 ; DX = address of MSG1
MOV AH, 09H ; DOS function 09H → print string until '$'
INT 21H ; Call DOS interrupt to print
; ---- Step 3: Print Second Message "WORLD" ----
MOV DX, OFFSET MSG2 ; DX = address of MSG2
MOV AH, 09H ; Again select print-string function
INT 21H ; Call DOS interrupt to print message
; ---- Step 4: Exit the Program ----
MOV AH, 4CH ; DOS function 4CH → terminate program
INT 21H ; Call interrupt to exit
MAIN ENDP
END MAIN
asm 26 lines, 1090 characters
If you want, I can also
✔ convert this into a flowchart,
✔ add mindmap,
✔ explain each instruction one-by-one,
✔ rewrite in MASM/TASM/FASM format,
✔ or add more example programs (input/output, arrays, loops, etc.).
Below is your FULLY ARRANGED, FULLY COMMENTED, LINE-BY-LINE EXPLAINED version of BOTH programs:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/20
✔ COM program (single segment, ORG 100h)
✔ EXE program (separate DATA and CODE segments)
✔ Original text preserved
✔ Major step comments exactly in your requested format:
✔ Title highlighted
; ---- Step X: … ----
✔ Clean formatting
✔ Assembled exactly like real 8086 MASM/TASM programs
🌟 2.6.1 COM PROGRAM (Single Segment Program)
Title: Add two numbers and store the result and carry
📌 Algorithm (COM Version)
1. Start execution at offset 100h
2. Initialize DS = CSEG because COM programs use one segment
3. Load first number NUM1 into register AL
4. Add second number NUM2 to AL
5. Store the 8-bit result in RESULT
6. Rotate left to move carry flag into AL (LSB)
7. Mask AL to get only carry bit (0 or 1)
8. Store carry value in CARRY
9. Terminate program using INT 21H / AH=4Ch
✨ FULL COM PROGRAM WITH COMMENTS
asm
; ============================================
; Title: ADD TWO NUMBERS AND STORE RESULT & CARRY
; ============================================
CSEG SEGMENT
ASSUME CS:CSEG, DS:CSEG, SS:CSEG
ORG 100h ; COM programs start at offset 100h
START:
; ---- Step 1: Initialize DS with same segment ----
MOV AX, CSEG ; Load the segment address of CSEG
MOV DS, AX ; DS = CSEG (code, data, stack all in this segment)
; ---- Step 2: Load first number ----
MOV AL, NUM1 ; AL = first number (15h)
; ---- Step 3: Add second number ----
ADD AL, NUM2 ; AL = AL + second number (20h)
; ---- Step 4: Store the sum ----
MOV RESULT, AL ; Save 8-bit result in memory
; ---- Step 5: Extract carry ----
RCL AL, 01 ; Rotate AL left through carry: CF → LSB of AL
AND AL, 00000001B ; Keep only bit0 (carry)
; ---- Step 6: Store the carry value ----
MOV CARRY, AL ; If carry: AL=01h, else AL=00h
; ---- Step 7: Terminate program ----
MOV AX, 4C00h ; AH=4Ch → terminate to DOS, AL=00 exit code
INT 21h
; ---- Data stored in same segment (COM Program Rule) ----
NUM1 DB 15h ; First number
NUM2 DB 20h ; Second number
RESULT DB ? ; Output result
CARRY DB ? ; Output carry
CSEG ENDS
END START
asm 31 lines, 1408 characters
🌟 2.6.2 EXE PROGRAM (Separate Segments)
Title: Add two numbers using EXE format
📌 Algorithm (EXE Version)
1. Program begins normally (no ORG 100h)
2. Load segment address of DATA into AX, then into DS
3. Move first number NUM1 into AL
4. Add second number NUM2
5. Store sum in RESULT
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/20
6. Rotate AL left to bring carry flag into bit 0
7. Mask AL to get only carry bit
8. Store in CARRY
9. Exit using DOS interrupt INT 21h / AH=4Ch
✨ FULL EXE PROGRAM WITH COMMENTS
asm
; =======================================================
; Title: ADD TWO 8-BIT NUMBERS (EXE FORMAT PROGRAM)
; ABSTRACT: Adds NUM1 and NUM2. Stores result in RESULT
; and carry (if any) in CARRY.
; REGISTERS USED: AX, AL, DS
; =======================================================
DATA SEGMENT
NUM1 DB 15h ; First number
NUM2 DB 20h ; Second number
RESULT DB ? ; Sum stored here
CARRY DB ? ; Carry stored here (00 or 01)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA ; Load address of DATA segment
MOV DS, AX ; DS = DATA
; ---- Step 2: Load first number ----
MOV AL, NUM1 ; AL = NUM1
; ---- Step 3: Add second number ----
ADD AL, NUM2 ; AL = AL + NUM2
; ---- Step 4: Store result ----
MOV RESULT, AL ; Store sum
; ---- Step 5: Extract carry ----
RCL AL, 01 ; Rotate carry flag into LSB of AL
AND AL, 00000001B ; Keep only carry bit
; ---- Step 6: Store carry ----
MOV CARRY, AL ; If carry exists AL=01h else AL=00h
; ---- Step 7: Exit program ----
MOV AX, 4C00h ; AH=4Ch → exit to DOS
INT 21h
CODE ENDS
END START
asm 34 lines, 1301 characters
✅ Both programs arranged
✅ Both fully commented
✅ Both maintain your required formatting style
If you want, I can also create:
✔ Flowcharts (your preferred style)
✔ Mindmap
✔ EXE vs COM comparison table
✔ More example programs with full comments
Just tell me!
Below is your Program 1 rewritten exactly in your required style:
✔ Clean formatting
✔ Line-by-line comments
✔ Major section comments using:
✔ Algorithm included
; ---- Step X: ... ----
✔ Title highlighted
🌟 Program 1: Difference Between MOV and XCHG Instructions
📌 Algorithm
1. Start program
2. Initialize the DS (Data Segment)
3. Load 1234H into AX
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/20
4. Exchange AH and AL using XCHG
5. Again load 1234H into AX
6. Load memory variable VAL (5678H) into BX
7. Exchange full registers AX ↔ BX
8. Exchange only AH with BL
9. Terminate program using INT 21H / AH = 4Ch
✨ FULL PROGRAM WITH DETAILED COMMENTS
asm
; ============================================================
; Program 1: Difference Between MOV and XCHG Instructions
; ============================================================
DATA SEGMENT
VAL DB 5678H ; VAL = 5678h → BH=56h, BL=78h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
MAINP:
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA ; Load address of DATA segment
MOV DS, AX ; DS = DATA (required to access VAL)
; ---- Step 2: Load a constant value into AX ----
MOV AX, 1234H ; AX = 1234h → AH = 12h, AL = 34h
; ---- Step 3: Exchange high and low bytes of AX ----
XCHG AH, AL ; Swap AH ↔ AL → AH = 34h, AL = 12h
; ---- Step 4: Reload AX again ----
MOV AX, 1234H ; AH = 12h, AL = 34h (restoring original)
; ---- Step 5: Move memory value into BX ----
MOV BX, VAL ; BX = 5678h → BH = 56h, BL = 78h
; ---- Step 6: Exchange AX and BX (full 16-bit swap) ----
XCHG AX, BX ; AX = 5678h, BX = 1234h
; ---- Step 7: Exchange only AH with BL ----
XCHG AH, BL ; After swap:
; AH = 34h, AL = 78h
; BH = 12h, BL = 56h
; ---- Step 8: Terminate Program ----
MOV AX, 4C00H ; AH=4Ch → DOS terminate function
INT 21H ; Exit to DOS
CODE ENDS
END MAINP
asm 31 lines, 1420 characters
If you want, I can also prepare:
✔ Flowchart of this program
✔ Simulation table (register changes step-by-step)
✔ Explanation of MOV vs XCHG in theory
✔ More assembly programs in the same commented style
Just tell me!
Below is Program 2 rewritten in your required style:
✔ Clean formatting
✔ Line-by-line comments
✔ Step-based comments using
✔ Algorithm included
; ---- Step X: ... ----
✔ Title highlighted
🌟 Program 2: Interchanging the Values of Two Memory Locations
📌 Algorithm
1. Start program
2. Initialize DS register to access data segment
3. Load VALUE1 into AL
4. Exchange AL ↔ VALUE2 using XCHG
5. Store the new AL into VALUE1
6. End the program
✨ FULL PROGRAM WITH DETAILED COMMENTS
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/20
asm
; ============================================================
; Program 2: Interchanging Two 8-bit Memory Values
; ============================================================
DATA SEGMENT
VALUE1 DB 0Ah ; First value = 0Ah
VALUE2 DB 14h ; Second value = 14h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA ; Load address of DATA segment
MOV DS, AX ; DS = DATA to access VALUE1, VALUE2
; ---- Step 2: Load first memory value into AL ----
MOV AL, VALUE1 ; AL = VALUE1 (0Ah)
; ---- Step 3: Exchange AL with VALUE2 ----
XCHG VALUE2, AL ; VALUE2 ↔ AL
; After swap:
; AL = 14h
; VALUE2 = 0Ah
; ---- Step 4: Store AL into VALUE1 ----
MOV VALUE1, AL ; VALUE1 = 14h
; ---- Step 5: Terminate Program ----
MOV AX, 4C00h ; AH = 4Ch → terminate to DOS
INT 21h ; Exit to operating system
CODE ENDS
END START
asm 27 lines, 1100 characters
If you'd like, I can also provide:
✔ Flowchart for this program
✔ Before/After memory table
✔ Alternate method using a temporary register
✔ COM version and EXE version
Just tell me!
Below is Program 3 rewritten in your required style:
✔ Clean formatting
✔ Line-by-line comments
✔ Major step comments using
✔ Algorithm included
; ---- Step X: ... ----
✔ Title highlighted
🌟 Program 3: Average of Two 8-bit Memory Values
📌 Algorithm
1. Start program
2. Initialize DS register
3. Load FIRST into AL
4. Add SECOND to AL
5. Clear AH to prepare for 16-bit division
6. Use ADC to add carry (if any) into AH
7. Load divisor 02h into BL
8. Perform DIV BL (AX ÷ BL → AL = quotient, AH = remainder)
9. Store AL (average) into AVGE
10. Terminate the program
✨ FULL PROGRAM WITH DETAILED COMMENTS
asm
; ============================================================
; Program 3: Find Average of Two Memory Values
; ============================================================
; Input: FIRST → first value
; SECOND → second value
; Output: AVGE → (FIRST + SECOND) / 2
; Registers used: DS, CS, AX, BL
; Ports: None
; ============================================================
DATA SEGMENT
FIRST DB 90h ; First number (example: 90h)
SECOND DB 78h ; Second number (example: 78h)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/20
AVGE DB ? ; Average will be stored here
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA ; Load base address of DATA segment
MOV DS, AX ; DS = DATA so we can access FIRST, SECOND, AVGE
; ---- Step 2: Load FIRST value ----
MOV AL, FIRST ; AL = FIRST (90h)
; ---- Step 3: Add SECOND value ----
ADD AL, SECOND ; AL = FIRST + SECOND (90h + 78h)
; ---- Step 4: Prepare for 16-bit division ----
MOV AH, 00h ; Clear AH (needed for DIV instruction)
ADC AH, 00h ; Add carry from earlier addition into AH
; ---- Step 5: Load divisor (2) ----
MOV BL, 02h ; BL = 2 → used to find average
; ---- Step 6: Divide AX by BL ----
DIV BL ; (AX ÷ 2)
; Quotient → AL (average)
; Remainder → AH
; ---- Step 7: Store the average ----
MOV AVGE, AL ; Save average into memory
; ---- Step 8: Terminate Program ----
MOV AX, 4C00h ; AH=4Ch → exit to DOS
INT 21h ; Return control to OS
CODE ENDS
END START
asm 40 lines, 1708 characters
If you want, I can also add:
✔ A flowchart (your preferred style)
✔ Step-by-step register table
✔ Alternate version using SHR instead of DIV
✔ COM version of this program
Just tell me!
Below is Program 4 rewritten exactly in your required style:
✔ Clean formatting
✔ Line-by-line comments
✔ Major section comments using
✔ Algorithm included
; ---- Step X: ... ----
✔ Title highlighted
✔ All your original text preserved, reorganized, explained clearly
🌟 Program 4: Convert Two ASCII Digits to Packed BCD
📌 Algorithm
1. Load ASCII digit 1 into BL
2. Load ASCII digit 2 into AL
3. Convert both ASCII values → unpacked BCD
Mask upper 4 bits using AND reg, 0Fh
4. Move first BCD digit into the upper nibble
Rotate BL left 4 positions using ROL BL, 04h
5. Pack both BCD digits into one byte
OR AL with BL
6. Resulting AL = packed BCD (example: 59 → 0101 1001)
✨ FULL PROGRAM WITH DETAILED COMMENTS
asm
; ============================================================
; Program 4: Convert Two ASCII Digits to Packed BCD
; ============================================================
; ABSTRACT:
; Program converts two ASCII digits ('5' and '9') into
; one packed BCD byte.
; BL contains ASCII '5'
; AL contains ASCII '9'
; Final packed BCD (59h) stored in AL.
;
; REGISTERS USED: CS, AL, BL, CL
; PORTS USED: None
; ============================================================
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/20
CODE SEGMENT
ASSUME CS:CODE
START:
; ---- Step 1: Load ASCII digits ----
MOV BL, '5' ; BL = ASCII 35h → 0011 0101b
MOV AL, '9' ; AL = ASCII 39h → 0011 1001b
; ---- Step 2: Convert both ASCII digits to unpacked BCD ----
AND BL, 0Fh ; BL = 0000 0101b → BCD 5
AND AL, 0Fh ; AL = 0000 1001b → BCD 9
; ---- Step 3: Move first BCD digit into upper nibble ----
MOV CL, 04h ; CL = 4 (number of bit rotates)
ROL BL, CL ; Rotate BL left by 4 bits:
; BL = 0101 0000b (5 shifted to upper nibble)
; ---- Step 4: Combine upper & lower nibbles to form packed BCD ----
OR AL, BL ; AL = 0101 1001b → packed BCD = 59h
; ---- Step 5: End program (optional exit if needed) ----
MOV AX, 4C00h ; DOS terminate
INT 21h
CODE ENDS
END START
asm 33 lines, 1387 characters
If you want, I can also provide:
✔ Full flowchart of ASCII → unpacked BCD → packed BCD
✔ Bit-level explanation for each rotate/mask
✔ Example for three-digit BCD packing
✔ COM & EXE versions
Just tell me!
Below is Program 6 rewritten completely in your required style:
✔ Clean formatting
✔ Line-by-line comments
✔ Major step comments using ; ---- Step X: ... ----
✔ Algorithm included
✔ Title highlighted
✔ Corrected small typo (PRICES → PRICE)
✔ Perfect MASM/TASM-ready formatting
🌟 Program 6: Apply Constant Inflation Factor to BCD Prices in Memory
📌 Algorithm
1. Initialize DS register
2. Point BX to the start of PRICE array
3. Set counter CX = number of items (8)
4. Loop for each price:
Load price from array
Add inflation factor (0Ah)
Apply DAA to adjust result to valid BCD
Store inflated price back
Move to next array element
5. Continue until CX becomes zero
6. Terminate program
✨ FULL PROGRAM WITH DETAILED COMMENTS
asm
; ============================================================
; Program 6: Add Inflation Factor to BCD Prices in Memory
; ============================================================
; ABSTRACT:
; A constant inflation factor (0Ah) is added to each BCD price
; stored in an array. The updated value overwrites the old one.
;
; REGISTERS USED: DS, CS, AX, BX, CX
; PORTS USED: None
; ============================================================
ARRAYS SEGMENT
PRICE DB 36h, 55h, 27h, 42h, 38h, 41h, 29h, 39h ; 8 BCD prices
ARRAYS ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:ARRAYS
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/20
START:
; ---- Step 1: Initialize Data Segment ----
MOV AX, ARRAYS ; Load base address of ARRAYS segment
MOV DS, AX ; DS = ARRAYS
; ---- Step 2: Set pointer to PRICE array ----
LEA BX, PRICE ; BX = offset of PRICE → points to first element
; ---- Step 3: Load loop counter (8 prices) ----
MOV CX, 0008h ; CX = 8 items to process
DO_NEXT:
; ---- Step 4: Read one price ----
MOV AL, [BX] ; AL = current BCD price (using indirect addressing)
; ---- Step 5: Add inflation factor ----
ADD AL, 0Ah ; Add constant inflation = 10 decimal
; ---- Step 6: Adjust result to valid BCD ----
DAA ; Decimal Adjust after Addition (corrects BCD)
; ---- Step 7: Store inflated price back ----
MOV [BX], AL ; Write updated price to memory
; ---- Step 8: Move pointer to next price ----
INC BX ; BX = BX + 1 (next array element)
; ---- Step 9: Decrement count and repeat ----
DEC CX ; Reduce number of remaining prices
JNZ DO_NEXT ; Loop until CX = 0
; ---- Step 10: End program ----
MOV AH, 4Ch ; Return to DOS
INT 21h
CODE ENDS
END START
asm 26 lines, 1042 characters
If you want, I can also provide:
✔ Flowchart for Program 6
✔ Before & After table for all 8 BCD prices
✔ Explanation of DAA with examples
✔ Version using ASCII instead of BCD
Just tell me!
Below is Program 7 rewritten exactly in your preferred style:
✔ Clean formatting
✔ Line-by-line comments
✔ Clear step blocks
✔ Algorithm included
✔ MASM/TASM ready
🌟 Program 7: Print Alphabets A–Z (ASCII A to Z)
📌 Algorithm
1. Load CX = 26 (total number of alphabets)
2. Load DL = ASCII of ‘A’ (41h)
3. Loop:
Set AH = 02h (DOS print character service)
Call INT 21h to print DL
Increment DL to get next alphabet
LOOP instruction decrements CX automatically
4. When CX = 0, exit program
5. Return to DOS
✨ FULL PROGRAM WITH DETAILED COMMENTS (Arranged + Explained)
asm
; ============================================================
; Program 7: Print Alphabets (A to Z) on Screen
; ============================================================
; REGISTERS USED : AX, CX, DX
; DOS SERVICE USED: INT 21H / AH = 02H (Display Character)
; ============================================================
CODE SEGMENT
ASSUME CS:CODE
MAINP:
; ---- Step 1: Load counter = 26 alphabets ----
MOV CX, 1Ah ; CX = 1Ah (26 decimal). LOOP will use this.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/20
; ---- Step 2: Load ASCII value of 'A' ----
MOV DL, 41h ; DL = 41h → ASCII for 'A'
NEXTC:
; ---- Step 3: DOS print-character service ----
MOV AH, 02h ; AH = 02h → Display character in DL
INT 21h ; Print the current alphabet on screen
; ---- Step 4: Go to next alphabet ----
INC DL ; DL = next character (A → B → C ...)
; ---- Step 5: LOOP handles iteration ----
LOOP NEXTC ; CX = CX – 1, if not zero → repeat
; ---- Step 6: Exit to DOS ----
MOV AX, 4C00h ; AH = 4Ch → Program terminate, AL = return code 00
INT 21h ; Call DOS
CODE ENDS
END MAINP
asm 26 lines, 1184 characters
If you want:
✔ I can also give ASCII table,
✔ Flowchart for this program,
✔ Version that prints a–z, A–Z, OR reverse alphabets Z–A,
✔ Version that prints letters with spaces or newline.
Below is Program 8 rewritten cleanly, corrected, fully arranged, and with line-by-line comments, exactly in your required format.
I also fixed the small typos in your original text:
✔ had an extra '
✔
'N'
had a space (NEXT P)
✔
LOOP NEXTP
INT 21 H spacing corrected
Everything else is preserved exactly.
🌟 Program 8 — Compare a Pair of Characters Entered From Keyboard
📌 Algorithm
1. Initialize DS (Data Segment).
2. Set counter CX = 3 (compare 3 pairs of characters).
3. Loop (3 times):
Read first character from keyboard → store in XX
Read second character from keyboard → store in YY
Move XX → BH, YY → BL
Compare BH and BL
4. If equal → display ‘Y’
5. If not equal → display ‘N’
6. Loop back until CX = 0
7. Exit to DOS
✨ FULL PROGRAM WITH DETAILED COMMENTS (Clean + Correct + Line-by-Line)
asm
; ============================================================
; Program 8: Compare two characters entered from keyboard
; Registers Used: AX, BX, CX, DX
; ============================================================
DATA SEGMENT
XX DB ? ; To store first input character
YY DB ? ; To store second input character
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
MAINP:
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA ; Load address of DATA segment
MOV DS, AX ; DS = DATA segment
; ---- Step 2: Counter for 3 comparisons ----
MOV CX, 03h ; Repeat 3 times
NEXTP:
; ---- Step 3: Read first character ----
MOV AH, 01h ; DOS service → read a char into AL
INT 21h ; Wait for keypress
MOV XX, AL ; Store first character in XX
; ---- Step 4: Read second character ----
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/20
MOV AH, 01h ; Wait for next keypress
INT 21h ; Second input → AL
MOV YY, AL ; Store second character in YY
; ---- Step 5: Load characters for comparison ----
MOV BH, XX ; Move first character to BH
MOV BL, YY ; Move second character to BL
CMP BH, BL ; Compare characters
JNE NOT_EQUAL ; Jump if not equal
EQUAL:
; ---- Step 6: Display 'Y' when equal ----
MOV AH, 02h ; Display character function
MOV DL, 'Y' ; Load 'Y'
INT 21h ; Print 'Y'
JMP CONTINUE ; Skip NOT_EQUAL part
NOT_EQUAL:
; ---- Step 7: Display 'N' when not equal ----
MOV AH, 02h ; Display character function
MOV DL, 'N' ; Load 'N'
INT 21h ; Print 'N'
CONTINUE:
LOOP NEXTP ; CX = CX - 1 → repeat if not zero
; ---- Step 8: Return to DOS ----
MOV AH, 4Ch ; Terminate program
INT 21h ; Return control to DOS
CODE ENDS
END MAINP
asm 48 lines, 1924 characters
If you want, I can also give:
✔ Flowchart for Program 8
✔ Example input/output
✔ An expanded logic breakdown of each instruction (like your earlier request)
✔ Corrected modern version using BIOS interrupts or C equivalent
Below is Program 9 fully rewritten, arranged, corrected, and commented line-by-line in your required style.
I kept all original text, only arranged and added detailed comments using:
✔
✔ Clean indentation
; ---- Step X: ... ----
✔ Correct structure
✔ Fixed minor formatting issues
Also included Algorithm exactly as you always request.
🌟 Program 9 — Find the Smallest and Largest Number in an Array
📌 Algorithm
1. Initialize DS (Data Segment).
2. Load the address of ARRAY in DI.
3. Load the first array value into AX.
4. Initialize:
DX = AX → largest
BX = AX → smallest
5. Set loop counter CX = 6 (because array has 6 elements).
6. Loop through all numbers:
Load array element into AX
Compare AX with BX → update smallest if needed
Compare AX with DX → update largest if needed
7. Move final smallest and largest values into memory variables.
8. Exit to DOS.
✨ FULL PROGRAM WITH DETAILED COMMENTS
asm
; ============================================================
; Program 9: Find the smallest and largest values in an array
; Registers Used: AX, BX, CX, DX, DI
; ============================================================
DATA SEGMENT
ARRAY DW -1, 2000, -4000, 32767, 500, 0 ; Array of signed numbers
LARGE DW ? ; To store largest number
SMALL DW ? ; To store smallest number
DATA ENDS
CODE SEGMENT
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/20
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Initialize DS ----
MOV AX, DATA ; Load address of DATA segment
MOV DS, AX ; DS = DATA
; ---- Step 2: Point DI to array ----
MOV DI, OFFSET ARRAY ; DI points to first element
; ---- Step 3: Load first array element ----
MOV AX, [DI] ; AX = ARRAY[0]
; ---- Step 4: Initialize Large and Small ----
MOV DX, AX ; DX = largest (initial)
MOV BX, AX ; BX = smallest (initial)
; ---- Step 5: Initialize loop counter ----
MOV CX, 6 ; Total 6 items in array
A1:
; ---- Step 6: Load the current array value ----
MOV AX, [DI] ; AX = ARRAY[i]
; ---- Step 7: Check if AX < BX (smaller?) ----
CMP AX, BX ; Compare with current smallest
JGE A2 ; If AX >= BX → not smaller → go check largest
; ---- Step 8: AX is smaller → update smallest ----
MOV BX, AX ; BX = new smallest
JMP A3 ; Skip large-check and continue loop
A2:
; ---- Step 9: Check if AX > DX (larger?) ----
CMP AX, DX ; Compare with current largest
JLE A3 ; If AX <= DX → not larger → continue
; ---- Step 10: AX is larger → update largest ----
MOV DX, AX ; DX = new largest
A3:
; ---- Step 11: Move DI to the next array element ----
ADD DI, 2 ; Each element is 2 bytes → next number
; ---- Step 12: Decrement loop and continue ----
LOOP A1 ; Continue until CX = 0
; ---- Step 13: Store results in memory ----
MOV LARGE, DX ; Store largest number
MOV SMALL, BX ; Store smallest number
; ---- Step 14: Exit to DOS ----
MOV AX, 4C00h ; DOS terminate function
INT 21h ; Exit program
CODE ENDS
END START
asm 52 lines, 2380 characters
If you want, I can also give:
✔ Flowchart for Program 9
✔ Memory-trace table (step-by-step list of values of AX, BX, DX, DI)
✔ C equivalent program
✔ Simplified explanation for exam revision
Just tell me!
Below is your Sample Program, rewritten in your required style, with:
✔ Original text preserved
✔ Full arrangement
✔ Line-by-line comments
✔ style
✔ Algorithm included
; ---- Step X: Description ----
✔ No changes to logic, only added explanation
✔ Proper formatting of DATA, STACK, CODE segments
🌟 Sample Program — Display a Message ("HAVE A NICE DAY!")
📘 Algorithm
1. Initialize DS (load DATA segment address into DS).
2. Load address of MESSAGE into DX.
3. Load function number 09H in AH (display string).
4. Call DOS interrupt 21H to display the message.
5. Load 4C00H into AX to return control to DOS.
6. Call interrupt 21H to terminate the program.
✨ FULL PROGRAM WITH COMMENTS (Your Required Format)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/20
asm
; ================================================================
; Sample Program: Display the message "HAVE A NICE DAY!"
; ================================================================
DATA SEGMENT
0002 0000 MESSAGE DB "HAVE A NICE DAY!$" ; '$' marks end of string for INT 21H/09H
DATA ENDS
STACK SEGMENT
0005 STACK 0400H ; ---- Step 1: Define 1KB stack area ----
STACK ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STACK
; -------- Machine Code shown for reference (as in your book) --------
; Offset Machine Code Source Code with Comments
; -------------------------------------------------------------------
; ---- Step 2: Initialize DS ----
0000 B8XXXX MOV AX, DATA ; Load DATA segment address into AX
0003 8ED8 MOV DS, AX ; Transfer AX → DS (DS now refers to DATA)
; ---- Step 3: Load address of MESSAGE ----
0005 BAXXXX MOV DX, OFFSET MESSAGE ; DX = address of MESSAGE string
; ---- Step 4: Load Function 09H to print message ----
0008 B409 MOV AH, 09H ; DOS function 09H prints string ending with '$'
; ---- Step 5: Execute DOS interrupt ----
000A CD21 INT 21H ; Print the message
; ---- Step 6: Exit program ----
000C B8004C MOV AX, 4C00H ; Load terminate program service number
000F CD21 INT 21H ; Return control to DOS
CODE ENDS
END
asm 28 lines, 1449 characters
If you want:
✅ Flowchart in your preferred style
✅ Explanation of each machine code byte (B8, 8E, CD, etc.)
✅ Memory map diagram
Just tell me!
Nice — great decision. Below I’ll give you a deep, practical, step-by-step guide to writing 8086 assembly code (MASM/TASM style), with the exact kinds of examples, algorithms,
and commented code you’ve been asking for. I’ll cover architecture basics, syntax and directives, addressing modes, common instruction families,
building/assembling/running, debugging tips, and many example programs (with the ; ---- Step X: ... ---- style and numbered algorithms you like). Use this as a study
path: read the concept, study the short example, then practise the exercises.
1) What you must understand first (overview)
1. CPU model & registers — 8086 has 16-bit registers: AX, BX, CX, DX (each can be AH/AL etc), SI, DI, BP, SP. Segment registers: CS, DS, SS, ES. IP and FLAGS.
2. Memory model — segmented memory. Logical address = segment:offset. COM programs load at offset 100h in a single segment; EXE programs use multiple segments.
3. Data sizes — byte (8-bit), word (16-bit). Numeric formats: binary, hex (ending h), signed vs unsigned.
4. Assembler directives — .MODEL, .STACK, .DATA, .CODE, ASSUME, ORG, END, etc (MASM/TASM style).
5. System calls / I/O — BIOS and DOS interrupts (e.g. INT 21h services) are common for simple programs.
6. Assembler toolchain — write .asm, assemble to object, link to .exe or produce .com. (If using emu8086 or DOSBox + MASM/TASM you run there.)
2) Basic file structure & standard template (EXE-style)
Algorithm (how a typical EXE program is structured):
1. Declare DATA segment (define variables).
2. Declare CODE segment.
3. Initialize DS to point to DATA.
4. Execute program logic.
5. Terminate with DOS INT 21h (AH=4Ch).
Example template (MASM/TASM style):
asm
; ===================================================
; Title: Template — EXE style
; ===================================================
DATA SEGMENT
; ---- data definitions ----
VAL1 DB 05h
VAL2 DB 03h
RES DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/20
; ---- Step 1: Initialize Data Segment ----
MOV AX, DATA
MOV DS, AX
; ---- Step 2: program logic ----
; ... your code here ...
; ---- Step 3: Terminate ----
MOV AX, 4C00h
INT 21h
CODE ENDS
END START
asm 22 lines, 524 characters
3) Syntax essentials (quick reference)
Comments: ; comment
Hex: 0FFh or 0Ah (no leading 0 required if letter included: Ah)
Binary literal: 01010101B
Immediate: MOV AL, 5
Memory operand: MOV AX, [OFFSET VAR] or MOV AL, VAR (depending on assembler)
Register size: MOV AX, BX (16-bit), MOV AL, BL (8-bit)
Label: LOOP1: then JMP LOOP1
4) Addressing modes
1. Immediate: MOV AX, 1234h
2. Register: MOV BX, AX
3. Direct memory: MOV AX, VAR
4. Register indirect: MOV AL, [BX] or MOV AX, [SI]
5. Based + displ: MOV AX, [BX+SI+4]
6. Relative (for jumps): JMP LABEL
5) Key instruction families and usage
Data transfer: MOV, XCHG, LEA, PUSH, POP
Arithmetic: ADD, SUB, INC, DEC, ADC, SBB, MUL, IMUL, DIV, IDIV
Logical/bit ops: AND, OR, XOR, NOT, SHL/SHR/SAL/SAR, ROL/ROR/RCL/RCR
String/block ops: MOVSB, MOVSW, CMPSB, SCASB, LODSB, STOSB with REP prefixes
Flow control: JMP, JE/JZ, JNE/JNZ, JL/JNGE, JG/JNLE, LOOP
Flags: CF (carry), ZF (zero), SF, OF — set by operations
6) How to convert ideas into code — stepwise method
1. Write an algorithm in plain English (numbered steps).
2. Choose registers and memory variables. Keep variable usage minimal and document which register does what.
3. Initialize DS, pointers, counters.
4. Implement operation(s) with careful attention to operand sizes and flags.
5. Handle special cases: overflow, sign, BCD adjust (DAA), division pre-conditions (AX must fit dividend for DIV reg8), etc.
6. Store results, clean up, exit.
7) Practical examples (with comments + your ; ---- Step X ---- style)
Example A — Hello World (print string using DOS)
Algorithm:
1. Initialize DS. 2. Load offset of string into DX. 3. AH = 09h. 4. INT 21h. 5. Exit.
asm
; ===================================================
; Program: Hello World (print string using INT 21h)
; ===================================================
DATA SEGMENT
MSG DB 'HELLO, WORLD!$' ; Terminated by '$' for DOS 09h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Init DS ----
MOV AX, DATA
MOV DS, AX
; ---- Step 2: Prepare string pointer ----
MOV DX, OFFSET MSG
MOV AH, 09h ; DOS print string
INT 21h ; Print
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/20
; ---- Step 3: Exit ----
MOV AX, 4C00h
INT 21h
CODE ENDS
END START
asm 21 lines, 596 characters
Example B — Add two bytes and store result + carry (you’ve seen variants)
Algorithm:
1. Load NUM1 into AL. 2. ADD NUM2. 3. Save AL to RESULT. 4. Rotate through carry and mask to get carry. 5. Store CARRY. 6. Exit.
asm
; ===================================================
; Add two 8-bit numbers, store sum and carry
; ===================================================
DATA SEGMENT
NUM1 DB 0x15
NUM2 DB 0x20
RESULT DB ?
CARRY DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
; ---- Step 1: Init DS ----
MOV AX, DATA
MOV DS, AX
; ---- Step 2: Add ----
MOV AL, NUM1
ADD AL, NUM2
MOV RESULT, AL
; ---- Step 3: Put carry in CARRY ----
RCL AL, 1 ; Rotate previous CF into LSB (CF already produced by ADD)
AND AL, 00000001B
MOV CARRY, AL
; ---- Step 4: Exit ----
MOV AX, 4C00h
INT 21h
CODE ENDS
END START
asm 28 lines, 687 characters
Example C — Loop over array and find min & max (compact)
(You’ve already got Program 9; keep practicing that exact pattern.)
Example D — Subroutine call & return
Algorithm:
1. Prepare parameters (registers or stack). 2. CALL SUB. 3. SUB uses RET.
asm
; ===================================================
; Program: simple subroutine that doubles AX
; ===================================================
DATA SEGMENT
VAL DW 0005h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AX, VAL ; AX = 5
CALL DOUBLE_AX ; call subroutine
MOV VAL, AX ; store back doubled value
MOV AX, 4C00h
INT 21h
DOUBLE_AX:
; ---- Step 1: Double AX ----
ADD AX, AX
RET
CODE ENDS
END START
asm 22 lines, 526 characters
Example E — Read a character, convert ASCII digit to numeric (0–9)
Algorithm:
1. Read char via INT 21h AH=01. 2. Subtract ASCII '0' (30h). 3. Use numeric result.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/20
asm
; ===================================================
; Read one ASCII digit and convert to binary (0..9)
; ===================================================
DATA SEGMENT
DIG DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AH, 01h ; read character (echoed)
INT 21h
SUB AL, '0' ; AL = numeric value 0..9
MOV DIG, AL
MOV AX, 4C00h
INT 21h
CODE ENDS
END START
asm 19 lines, 454 characters
Example F — Pack two ASCII digits into packed BCD (we already did Program 4; similar pattern)
Use AND to mask, ROL to shift nibble, OR to combine, then DAA if needed.
8) Important technical notes & gotchas
DIV and IDIV: dividend must be in AX (or DX:AX for word divisor). For DIV BL, the dividend is AL (actually for 8-bit divisor you divide AX by BL? Correction: For 8-bit divisor,
dividend must be AX; quotient in AL. Always zero or prepare high register appropriately). Always zero AH before DIV for 8-bit division: MOV AH,0 then DIV BL.
Signed vs unsigned: use IDIV / IMUL for signed operations.
BCD: after decimal arithmetic use DAA (add adjust) or AAA/AAD as appropriate.
Watch sizes: mixing 8-bit and 16-bit registers without care causes bugs.
Stack discipline for CALL/RET: if you push parameters, clean on correct side (stdcall vs cdecl) — in assembly you choose.
9) Assembling & running (practical)
Typical steps (conceptual — specific commands depend on tool):
1. Write [Link].
2. Assemble: ml [Link] (MASM) or tasm [Link] (TASM).
3. Link: link [Link] (to produce .exe).
4. Run inside DOSBox or emulator, or produce .com with ORG 100h and a one-segment program.
(If you want exact command lines for MASM/TASM/emu8086, tell me which tool you’ll use and I’ll give explicit commands.)
10) Debugging techniques
1. Insert short debug prints: print register values as hex by converting nibble to ASCII.
2. Use an emulator with step/tracing (emu8086, Bochs, DOSBox with DEBUG).
3. Keep programs small and test each routine separately.
4. Use memory dumps to verify array contents.
5. Track flags after operations via conditional jumps.
11) Practice plan (learn by doing)
Week 1 — basics:
Write Hello World.
Read a character, echo it.
Do small arithmetic and print results.
Week 2 — memory & arrays:
Implement array sum.
Min/max (Program 9).
Average of numbers (Program 3).
Week 3 — string routines:
Write string copy and length using REP instructions.
Compare strings.
Convert ASCII strings to numbers and vice versa.
Week 4 — subroutines + I/O:
Implement and call subroutines with parameters on stack.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/20
Read and write files (INT 21h file services) — intermediate.
Advanced: work with interrupts, BIOS, graphics mode, and interfacing with C.
12) Common small projects to get confident
1. Calculator: +, −, ×, ÷ with menu and ASCII input.
2. String sorting (eight strings).
3. Simple BCD arithmetic library.
4. Convert integer to decimal ASCII (write your own routine).
5. Tiny shell: read command and call different subroutines.
13) Learning resources & tips
Read the 8086 programmer’s manual (for instruction encodings and flags).
Use an emulator (emu8086, DOSBox with MASM) and step through code instruction-by-instruction.
Start with EXE programs (easier to debug than COM).
Keep variable names and register use consistent: comment each register role.
14) Exercises (do these now)
1. Write a program that reads two digits (ASCII), adds them (as numbers), and prints the sum as ASCII (handle carry up to 18).
2. Convert Program 3 (average) to use SHR instead of DIV when you can (hint: for unsigned values you can use SHR AL, 1 but must handle carry bit).
3. Implement a routine that converts a 16-bit unsigned number (0..65535) into its decimal ASCII representation (5 digits).
4. Write a routine that reverses a zero-terminated string in place.
If you want, I’ll take one exercise (pick any) and write a fully commented solution in the exact format you like (algorithm + code + ; ---- Step X ---- comments + a small
flowchart).
Want me to:
• produce a guided, step-by-step lesson where I give you one small program, then expand it each step?
• or start by giving the full solution to Exercise 1 right now (with code, explanation, and flowchart)?
Pick one and I’ll write it out in the exact style you prefer.
Great — since you already have the full explanation of all 8086 registers, now I will give you the next MOST IMPORTANT thing for becoming perfect in 8086 Assembly
programming:
⭐ HOW TO START WRITING 8086 ASSEMBLY CODE — COMPLETE BEGINNER-TO-MASTER GUIDE
This is the deep, step-by-step instruction manual you need.
I will explain:
✅ How an 8086 program is structured
✅ What each section means (DATA / CODE / STACK)
✅ How to use registers in real programs
✅ How to write instructions correctly
✅ Exact flow to think while writing any program
✅ How to debug mentally
✅ How to write your own new programs
⭐ PART 1 — BASIC STRUCTURE OF AN 8086 PROGRAM
Every 8086 program has 3 sections:
nginx
DATA SEGMENT
; variables here
DATA ENDS
CODE SEGMENT
; instructions here
CODE ENDS
STACK SEGMENT
; optional
STACK ENDS
unknown 9 lines, 130 characters
✔️ Why do we need segments?
8086 works on segmented memory, so we divide:
Segment Purpose
DATA To store variables
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/20
Segment Purpose
CODE To store instructions
STACK Used by PUSH, POP, CALL, RET
⭐ PART 2 — MANDATORY INSTRUCTIONS
(These must appear in every program)
✔️ 1. Load Data Segment
powershell
MOV AX, DATA
MOV DS, AX
unknown 2 lines, 24 characters
Because CPU cannot use DS directly — we must load DS through AX.
⭐ PART 3 — WRITING INSTRUCTIONS
8086 instruction format:
bash
OPCODE destination, source
unknown 1 line, 28 characters
Examples:
powershell
MOV AX, BX
ADD AL, 20h
SUB BX, AX
unknown 3 lines, 34 characters
⭐ PART 4 — THINKING PROCESS TO WRITE ANY 8086 PROGRAM
Whenever you want to write ANY program, follow these 6 steps:
🟩 STEP 1 — Identify the INPUT
Examples:
Two numbers from memory
A string from keyboard
A value in AL
An array
🟩 STEP 2 — Identify what PROCESS you need
Examples:
Addition
Looping
Compare values
Checking largest/smallest
Moving data
🟩 STEP 3 — Identify which REGISTERS will be used
General idea:
Task Registers
Arithmetic AX, BX, CX, DX
Loops CX
Array access SI, DI, BX
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/20
Task Registers
Division AX, DX
Output DL, AH=02h
Input AL, AH=01h
🟩 STEP 4 — Choose ADDRESSING MODE
8086 has 5 addressing modes:
1️⃣ Immediate
MOV AL, 20h
2️⃣ Register
MOV AX, BX
3️⃣ Direct memory
MOV AL, [2000h]
4️⃣ Register indirect
,
MOV AL, [SI] MOV AX, [BX]
5️⃣ Based-index
MOV AL, [BX + SI]
🟩 STEP 5 — Write instructions
Start with initialization:
powershell
MOV AX, DATA
MOV DS, AX
unknown 2 lines, 24 characters
Then write body of program.
🟩 STEP 6 — Exit Program
powershell
MOV AH, 4Ch
INT 21h
unknown 2 lines, 20 characters
⭐ PART 5 — ESSENTIAL 8086 INSTRUCTIONS YOU MUST MASTER
Below are the core instructions used in almost every program.
🟦 MOV — Move Data
css
MOV AX, BX
MOV AL, [SI]
MOV [2000], AL
unknown 3 lines, 39 characters
🟦 ADD / SUB — Arithmetic
sql
ADD AX, BX
SUB AL, 1
unknown 2 lines, 21 characters
🟦 INC / DEC — Increment / Decrement
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/20
sql
INC BX
DEC CX
unknown 2 lines, 14 characters
🟦 CMP — Compare
Sets flags; does NOT change operand.
objectivec
CMP AX, BX
unknown 1 line, 11 characters
🟦 Jumps (Conditional + Unconditional)
Conditional:
css
JE label
JNE label
JG label
JL label
JGE label
unknown 5 lines, 47 characters
Unconditional:
css
JMP label
unknown 1 line, 10 characters
🟦 LOOP Instruction
Automatically:
CX = CX – 1
If CX ≠ 0 → jump to label
css
MOV CX, 10
L1: ; body
LOOP L1
unknown 3 lines, 31 characters
🟦 DAA — Decimal Adjust After Addition
Used only after BCD addition.
🟦 INT 21H — DOS Interrupt
Used for input/output.
AH Meaning
01h Input character to AL
02h Output character in DL
09h Display string
⭐ PART 6 — HOW TO WRITE YOUR VERY FIRST SIMPLE PROGRAM
Example: Add two numbers and display result
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/20
✔️ Step-by-step explanation:
powershell
DATA SEGMENT
NUM1 DB 05h
NUM2 DB 03h
RES DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AL, NUM1
ADD AL, NUM2
MOV RES, AL
MOV AH, 4Ch
INT 21h
CODE ENDS
END START
unknown 17 lines, 241 characters
⭐ PART 7 — HOW TO THINK IN ASSEMBLY (MOST IMPORTANT)
When solving ANY Assembly problem:
✔️ Always ask:
1. Where is my input? (memory, keyboard, register?)
2. Which registers will I use for storage?
3. Do I need a loop? If yes, set CX.
4. Do I need comparison? → CMP + Jcc
5. Do I need array traversal? → SI / DI / BX
6. Do I need BCD adjustment? → DAA
7. Finally: store result → memory / register
8. Exit to DOS
⭐ PART 8 — WHAT YOU SHOULD PRACTICE (MY RECOMMENDATION)
To become PERFECT:
📌 Write these programs yourself:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
5. Find largest of 3 numbers
6. Find smallest in an array
7. Count vowels in a string
8. Reverse a string
9. Compare two characters
10. Print A–Z
I can give all programs + full explanations whenever you want.
⭐ If you want, I can now create:
✅ Full Assembly Programming Notes
✅ 8086 Syntax Handbook
✅ Flowcharts for each program
✅ Step-by-step algorithms
✅ Memory diagrams
✅ Execution-trace diagrams
Just tell me → what do you want next?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/20