Assembly Language Operations: 16-bit and 32-bit Processing

Bit Multiplication

1. Move the 16-bit value at memory address 3000 to AX.

MOV AX, [3000] loads the 16-bit value from memory address 3000 into the AX register.

2. Move the 16-bit value at memory address 3002 to BX.

MOV BX, [3002] loads the 16-bit value from memory address 3002 into the BX register.

3. Multiply AX by BX.

MUL BX performs the multiplication of AX and BX. The result is a 32-bit product in the DX:AX register pair, where DX holds the high word and AX holds the low word.

4. Move the low word of the product (AX) to memory address 3004.

MOV [3004], AX stores the low word of the product (AX) at memory address 3004.

5. Move the high word of the product (DX) to AX.

  • MOV AX, DX moves the high word of the product from DX to AX.

6. Move the high word of the product (AX) to memory address 3006.

  • MOV [3006], AX stores the high word of the product (AX) at memory address 3006.

7. Halt the program.

HLT halts the execution of the program.

Sorting in Ascending Order

1. Move immediate value 500 to SI.

MOV SI, 500 initializes the SI register with the value 500.

2. Move the byte at the memory location pointed to by SI to CL.

  • MOV CL, [SI] loads the byte at the memory address in SI into the CL register.

3. Decrement CL.

DEC CL decrements the value in CL.

4. Move immediate value 500 to SI.

MOV SI, 500 initializes the SI register with the value 500.

5. Move the byte at the memory location pointed to by SI to CH.

  • MOV CH, [SI] loads the byte at the memory address in SI into the CH register.

6. Decrement CH.

  • DEC CH decrements the value in CH.

7. Increment SI.

  • INC SI increments the value in SI.

8. Move the byte at the memory location pointed to by SI to AL.

  • MOV AL, [SI] loads the byte at the memory address in SI into the AL register.

9. Increment SI.

  • INC SI increments the value in SI.

10. Compare AL with the byte at the memory location pointed to by SI.

  • CMP AL, [SI] compares the values in AL and the memory address in SI.

11. Jump to label 41C if the carry flag is set (JC 41C).

  • JC 41C jumps to label 41C if the carry flag is set (indicates a carry in the previous CMP operation).

12. Exchange the values in AL and the byte at the memory location pointed to by SI.

XCHG AL, [SI] exchanges the values in AL and the memory address in SI.

13. Decrement SI.

DEC SI decrements the value in SI.

14. Exchange the values in AL and the byte at the memory location pointed to by SI.

XCHG AL, [SI] exchanges the values in AL and the memory address in SI.

15. Increment SI.

INC SI increments the value in SI.

16. Decrement CH.

DEC CH decrements the value in CH.

17. Jump to label 40F if CH is not zero (JNZ 40F).

  • JNZ 40F jumps to label 40F if the zero flag is not set (indicates CH is not zero).

18. Decrement CL.

  • DEC CL decrements the value in CL.

19. Jump to label 407 if CL is not zero (JNZ 407).

  • JNZ 407 jumps to label 407 if the zero flag is not set (indicates CL is not zero).

20. Halt the program.

HLT halts the execution of the program.

Bit Addition

1. Initialize AX register to 0000.

AND AX, 0000 clears the AX register.

2. Move immediate value 0600H to BX.

  • MOV BX, 0600H loads the value 0600H into the BX register.

3. Move immediate value 0500H to SI.

  • MOV SI, 0500H loads the value 0500H into the SI register.

4. Move immediate value 0550H to DI.

MOV DI, 0550H loads the value 0550H into the DI register.

5. Move the word at the address in SI to AX.

MOV AX, [SI] loads the 16-bit value stored at the memory location pointed to by SI into the AX register.

6. Move the word at the address in DI to AX.

MOV AX, [DI] loads the 16-bit value stored at the memory location pointed to by DI into the AX register.

7. Move the value in AX to the memory location pointed to by BX.

MOV [BX], AX stores the value in AX at the memory location pointed to by BX.

8. Clear AX register to 0000H.

  • MOV AX, 0000H sets the AX register to 0000H.

9. Add with carry the immediate value 0000H to AX.

  • ADC AX, 0000H adds the immediate value 0000H to AX, including the carry flag.

10. Move the value in AX to the memory location at (BX+2).

  • MOV [BX+2], AX stores the value in AX at the memory location two bytes ahead of BX.

11. Halt the program.

  • HLT halts the execution of the program.

Bit Subtraction

1. Clear the carry flag.

CLC clears the carry flag.

2. Move immediate value 0900H to BX.

  • MOV BX, 0900H loads the value 0900H into the BX register.

3. Move immediate value 0700H to SI.

  • MOV SI, 0700H loads the value 0700H into the SI register.

4. Move immediate value 0800H to DI.

MOV DI, 0800H loads the value 0800H into the DI register.

5. Move the word at the address in SI to AX.

MOV AX, [SI] loads the 16-bit value stored at the memory location pointed to by SI into the AX register.

6. Subtract with borrow the value at the address in DI from AX.

SBB AX, [DI] performs a subtract with borrow operation on AX and the 16-bit value at the memory location pointed to by DI.

7. Move the value in AX to the memory location pointed to by BX.

  • MOV [BX], AX stores the value in AX at the memory location pointed to by BX.

8. Halt the program.

HLT halts the execution of the program.

Bit Addition Code Segment

1. Define a data segment (DATA SEGMENT) to store data, including three 16-bit unsigned integers (N1, N2, N3).

2. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

3. Define a code segment (CODE SEGMENT) to write the main program logic.

4. Set the starting point of the code segment (START:).

5. Move the address of the data segment to the AX register using the MOV instruction.

6. Set the data segment register (DS) to the value in the AX register.

7. Clear the AX register using XOR AX, AX.

8. Move the value in AX to the BX register.

9. Move the value in N1 to the AX register.

10. Add the value in N2 to AX.

11. Move the result (AX) to the variable N3.

12. Check the carry flag (JNC STOP).

a. If there is no carry, jump to the STOP label.

b. If there is a carry, increment BX.

13. STOP label:

a. Move the result (AX) to the CX register.

b. Set AH to 4CH (DOS termination function).

c. Trigger interrupt 21H to terminate the program.

14. End the code segment (CODE ENDS).

15. End the program with the label “END START.”

Bit Addition Code Segment

1. Define a data segment (DATA SEGMENT) to store data, including an array of two double-words (LIST) and two 16-bit unsigned integers (N3, N4).


2. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

3. Define a code segment (CODE SEGMENT) to write the main program logic.

4. Set the starting point of the code segment (START:).

5. Move the address of the data segment to the AX register using the MOV instruction.

6. Set the data segment register (DS) to the value in the AX register.

7. Clear the AX register using XOR AX, AX.

8. Move the value in AL to the CL register.

9. Load the first double-word from the array (LIST) into the AX register.

10. Add the second double-word from the array (LIST+4) to AX.

11. Move the result (AX) to the variable N3.

12. Load the third double-word from the array (LIST+2) into the AX register.

13. Add the fourth double-word from the array (LIST+6) to AX.

14. Move the result (AX) to the variable N4.

15. Check the carry flag (JNC STOP).

a. If there is no carry, jump to the STOP label.

b. If there is a carry, increment the counter (CL).

16. STOP label:

a. Set AH to 4CH (DOS termination function).

b. Trigger interrupt 21H to terminate the program.

17. End the code segment (CODE ENDS).

18. End the program with the label “END START.”

Bit Subtraction Code Segment

1. Define a data segment (DATA SEGMENT) to store data, including an array of two double-words (LIST) and two 16-bit unsigned integers (N3, N4).

2. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

3. Define a code segment (CODE SEGMENT) to write the main program logic.

4. Set the starting point of the code segment (START:).

5. Move the address of the data segment to the AX register using the MOV instruction.

6. Set the data segment register (DS) to the value in the AX register.

7. Clear the AX register using XOR AX, AX.

8. Move the value in AL to the CL register.

9. Load the first double-word from the array (LIST) into the AX register.

10. Add the second double-word from the array (LIST+4) to AX.

11. Move the result (AX) to the variable N3.

12. Load the third double-word from the array (LIST+2) into the AX register.

13. Add the fourth double-word from the array (LIST+6) to AX.

14. Move the result (AX) to the variable N4.

15. Check the carry flag (JNC STOP).

a. If there is no carry, jump to the STOP label.

b. If there is a carry, increment the counter (CL).

16. STOP label:

a. Set AH to 4CH (DOS termination function).

b. Trigger interrupt 21H to terminate the program.

17. End the code segment (CODE ENDS).

18. End the program with the label “END START.”

Bit Multiplication Code Segment

1. Define a data segment (DATA SEGMENT) to store data, including two 16-bit unsigned integers (N1, N2) and another variable (N3).

2. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

3. Define a code segment (CODE SEGMENT) to write the main program logic.

4. Set the starting point of the code segment (START:).

5. Move the immediate value 4343H to the AX register.

6. Move the immediate value 1111H to the BX register.

7. Trigger interrupt 3 (INT 3).

8. End the code segment (CODE ENDS).

9. End the program with the label “END START.”

Data Segment and Sorting Code

1. Define a data segment (DATA SEGMENT) to store the data, specifically a sequence of hexadecimal values (STRING1).

2. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

3. Define a code segment (CODE SEGMENT) to write the main program logic.

4. Set the starting point of the code segment (START:).

5. Move the address of the data segment to the AX register using the MOV instruction.

6. Set the data segment register (DS) to the value in the AX register.

7. Initialize CH to 04H (outer loop counter).

8. Start the outer loop (UP2) with a label.

a. Initialize CL to 04H (inner loop counter).

b. Load the effective address of STRING1 into SI.

c. Start the inner loop (UP1) with a label.

i. Move the value at the memory location pointed by SI (AL) and the next byte (BL) into registers.

ii. Compare AL and BL.

iii. If AL is not less than BL, jump to the DOWN label.

iv. Swap the values at the memory locations pointed by SI and SI+1.

v. Continue to DOWN label.

d. Increment SI, decrement CL, and repeat the inner loop until CL is not zero.

e. Decrement CH and repeat the outer loop until CH is not zero.

9. Trigger interrupt 3 (INT 3) – This is a breakpoint interrupt and is often used for debugging.

10. End the code segment (CODE ENDS).

11. End the program with the label “END START.”

Searching Algorithm

1. Define a data segment (DATA SEGMENT) to store data, including an array of hexadecimal values (STRING1), messages (MSG1 and MSG2), and a search element (SE).

2. Define a macro (PRINT) that prints a message using DOS interrupt 21H (INT 21H).

3. Use the ASSUME directive to specify the code and data segments (CS:CODE, DS:DATA).

4. Define a code segment (CODE SEGMENT) to write the main program logic.

5. Set the starting point of the code segment (START:).

6. Move the address of the data segment to the AX register using the MOV instruction.

7. Set the data segment register (DS) to the value in the AX register.

8. Move the search element (SE) to the AL register.

9. Load the effective address of the array (STRING1) into SI.

10. Initialize the loop counter (CX) to 04H (number of elements in the array).

11. Start a loop (UP) with a label.

a. Move the value at the memory location pointed by SI to BL.

b. Compare AL with BL.

c. If equal (Z), jump to the label FO (found).

d. Increment SI, decrement CX, and repeat the loop until CX is not zero.

12. Print the “NOT FOUND” message using the PRINT macro and jump to END1.

13. Found label (FO):

a. Print the “FOUND” message using the PRINT macro.

14. END1 label:

a. Trigger interrupt 3 (INT 3) – This is a breakpoint interrupt and is often used for debugging.

15. End the code segment (CODE ENDS).

16. End the program with the label “END START.”

Algorithm for Simple Macro Processor

1. Open the macro definition file (‘macro.txt’) for reading (‘fpm’).

2. Open the input file (‘minput.txt’) for reading (‘fpi’).

3. Open the output file (‘moutput.txt’) for writing (‘fpo’).

4. Initialize variables: ‘m’ (to track the number of macros), ‘i’, ‘j’, and ‘flag’.

5. Read each line from the macro definition file (fpm):

a. Tokenize the line using spaces.

b. If the first token is “MACRO”, store the second token (macro name) in the ‘mac’ array and increment ‘m’.

6. Read a line from the input file (‘fpi’).

7. While not at the end of the file:

a. Set ‘flag’ to 0.

b. Copy the current line to ‘str1’.

c. For each macro in the ‘mac’ array:

i. If the current line matches a macro name:

  • Set ‘flag’ to 1.
  • Rewind the macro definition file.
  • While not at the end of the macro definition file:
  • Read a line from the macro definition file.
  • Tokenize the line to get the macro name.
  • If the macro name matches the current macro:
  • Read and write lines from the macro definition file to the output file until “MEND” is encountered.
  • Break from the inner loop.

d. If ‘flag’ is 0, write the current line to the output file.

e. Read the next line from the input file.

8. Close all open files (‘fpm’, ‘fpi’, and ‘fpo’).

Pass One Macro

1. Open the input file (‘inputm.txt’) for reading (fp1).

2. Initialize an array of file pointers (fp) to store the macro definitions.

3. Read the first line from fp1 to get the label, opcode, and operand.

4. While the opcode is not “END”:

a. If the opcode is “MACRO”:

i. Open a new file with the given operand as the filename and store the file pointer in the ‘fp’ array.

ii. Increment the array index (m).

iii. Read the next line from fp1 to get the label, opcode, and operand.

iv. While the opcode is not “MEND”:

  • Write the label, opcode, and operand to the current macro file (fp[m-1]).
  • Read the next line from fp1.

v. Close the current macro file (fp[m-1]).

b. Read the next line from fp1 to get the label, opcode, and operand.

5. Close the input file (fp1).

Pass Two Macro

1. Open the input file (‘inputm.txt’) for reading (‘fp1’).

2. Open the output file (‘macro_out.txt’) for writing (‘fp2’).

3. Initialize an array of file pointers (‘fp’) to store the macro files.

4. Read the first line from ‘fp1’ to get the label, opcode, and operand.

5. While the opcode is not “END”:

a. If the opcode is “CALL”:

i. Open the macro file with the given operand as the filename for reading (fp[m]).

ii. Increment the array index (‘m’).

iii. Read the first line from the macro file to get the label, opcode, and operand.

iv. While not at the end of the macro file (‘feof(fp[m])’):

  • Write the label, opcode, and operand to the output file (fp2).
  • Read the next line from the macro file.

v. Close the current macro file (‘fp[m]’).

b. If the opcode is not “CALL”:

  • Write the label, opcode, and operand to the output file (‘fp2’).

c. Read the next line from ‘fp1’ to get the label, opcode, and operand.

6. Close all open files (‘fp1’, ‘fp2’, and any open macro files in ‘fp’).

Absolute Loader Algorithm

1. Open the file ‘objectcode.txt’ for reading (fp).

2. Prompt the user to enter the program name and read it into the variable ‘name’.

3. Read the first line from the file (‘line’).

4. Extract the program name from the second to the seventh characters of ‘line’ and store it in ‘name1’.

5. Print the program name obtained from the object code (‘name1’).

6. Check if the user-entered program name (‘name’) matches the program name from the object code (‘name1’).

a. If they match, continue processing; otherwise, exit the program.

b. Read the next line from the file (‘line’).

7. Loop until the end of the file is reached:

a. If the first character of the line is ‘T’:

i. Extract the starting address from the third to eighth characters of ‘line’ and convert it to an integer (‘staddr1’).

ii. Initialize the loop variable ‘i’ to 12 (the position where object code records start in the line).

iii. Loop until the end of the record (‘$’ character):

  • If the current character is not ‘$’, print the address and the two characters of object code.
  • Increment ‘staddr1’ and move ‘i’ two positions forward.

b. If the first character of the line is ‘E’, print the execution address obtained from the line.

c. Read the next line from the file (‘line’).

8. Close the file.

Algorithm for Pass 1 Assembler

Begin

read first input line

if OPCODE = ‘START’ then

begin

save #[Operand] as starting address

initialize LOCCTR to starting address

write line to intermediate file

read next input line

end {if START}

else

initialize LOCCTR to 0

While OPCODE != ‘END’ do

begin

if this is not a comment line then

begin

if there is a symbol in the LABEL field then

begin

search SYMTAB for LABEL

if found then

set error flag (duplicate symbol)

else

insert (LABEL, LOCCTR) into SYMTAB

end {if symbol}

search OPTAB for OPCODE

if found then

add 3 (instruction length) to LOCCTR

else if OPCODE = ‘WORD’ then

add 3 to LOCCTR

else if OPCODE = ‘RESW’ then

add 3 * #[OPERAND] to LOCCTR

else if OPCODE = ‘RESB’ then

add #[OPERAND] to LOCCTR

else if OPCODE = ‘BYTE’ then

begin

find length of constant in bytes

add length to LOCCTR

end {if BYTE}

else

set error flag (invalid operation code)

end {if not a comment}

write line to intermediate file

read next input line

end {while not END}

write last line to intermediate file

Save (LOCCTR – starting address) as program length

End {pass1}

Algorithm for Pass 2 Assembler

Begin

read 1st input line from intermediate file

if OPCODE = ‘START’ then

begin

write listing line

read next input line

end {if START}

write Header record to object program

initialize 1st Text record

while OPCODE != ‘END’ do

begin

if this is not comment line then

begin

search OPTAB for OPCODE

if found then

begin

if there is a symbol in OPERAND field then

begin

search SYMTAB for OPERAND

if found then

store symbol value as operand address

else

begin

store 0 as operand address

set error flag (undefined symbol)

end

end {if symbol}

else

store 0 as operand address

assemble the object code instruction

end {if opcode found}

else if OPCODE = ‘BYTE’ or ‘WORD’ then

convert constant to object code

if object code doesn’t fit into current Text record then

begin

Write text record to object code

initialize new Text record

end

add object code to Text record

end {if not comment}

write listing line

read next input line

end {while not END}

Write last Text record to object program

Write End record to object program

Write last listing line.

End {Pass 2}