**Program 10: Matrix Multiplication**
```assembly
.model small
.stack 100h
.data
matrix1 dw 1, 2, 3, 4
matrix2 dw 5, 6, 7, 8
result dw 0, 0, 0, 0
.code
main proc
mov ax, 0 ; Initialize AX to 0
outer_loop:
mov cx, 2 ; Set loop counter for rows
inner_loop:
mov bx, ax ; Copy AX to BX (row index)
add bx, cx ; Add CX to BX (row index + column index)
mov dx, 0 ; Initialize DX (column index)
multiply_loop:
mov si, dx ; Copy DX to SI (column index)
add si, cx ; Add CX to SI (row index + column index)
mov ax, [matrix1 + bx * 2] ; Load element from matrix1
mov bx, [matrix2 + si * 2] ; Load element from matrix2
imul bx ; Multiply and store result in AX
add [result + dx * 2], ax ; Add result to the appropriate location in the result matrix
inc dx ; Increment column index
cmp dx, 2 ; Compare with 2 (matrix dimension)
jl multiply_loop ; Jump if less than 2
inc bx ; Increment row index
loop inner_loop ; Decrement CX and loop if not zero
inc ax ; Increment AX (column index)
cmp ax, 2 ; Compare with 2 (matrix dimension)
jl outer_loop ; Jump if less than 2
; Print result matrix
mov ax, 0 ; Initialize AX (row index)
mov cx, 2 ; Set loop counter for rows
print_loop:
mov dx, 0 ; Initialize DX (column index)
print_inner_loop:
mov bx, ax ; Copy AX to BX (row index)
add bx, dx ; Add DX to BX (row index + column index)
mov ax, [result + bx * 2] ; Load element from result matrix
call print_number
inc dx ; Increment column index
cmp dx, 2 ; Compare with 2 (matrix dimension)
jl print_inner_loop ; Jump if less than 2
call print_newline ; Print newline
inc ax ; Increment AX (row index)
loop print_loop ; Decrement CX and loop if not zero
mov ah, 4Ch ; Exit program
int 21h
main endp
print_number proc
push bx
push cx
mov bx, ax
mov cx, 10
mov ah, 0 ; Initialize quotient (AH) to 0
mov dx, 0 ; Initialize remainder (DX) to 0
reverse_loop:
div cx ; Divide BX by 10, result in AX, remainder in DX
add dl, 30h ; Convert remainder to ASCII
push dx ; Push ASCII character onto stack
mov dx, 0 ; Clear DX for next iteration
test ax, ax ; Check if quotient is 0
jnz reverse_loop ; If not zero, loop again
print_loop:
pop dx ; Pop ASCII character from stack
mov ah, 02h ; DOS function to print character
int 21h ; Print character
loop print_loop ; Loop until characters are printed
pop cx
pop bx
ret
print_number endp
print_newline proc
mov dl, 0Ah ; ASCII code for newline
mov ah, 02h ; DOS function to print character
int 21h
ret
print_newline endp
end main
```
Explanation:
1. `.model small` and `.stack 100h`: Memory model and stack size definitions.
2. `.data` section: Declares the data segment.
- `matrix1 dw 1, 2, 3, 4`: Defines a 2x2 matrix named "matrix1".
- `matrix2 dw 5, 6, 7, 8`: Defines a 2x2 matrix named "matrix2".
- `result dw 0, 0, 0, 0`: Defines a 2x2 matrix named "result" for storing the multiplication result.
3. `.code` section: Contains the main code.
- `mov ax, 0`: Initialize AX to 0 (row index).
- `outer_loop:`: Label for the outer loop (rows).
- `mov cx, 2`: Set CX to 2 (loop counter for rows).
- `inner_loop:`: Label for the inner loop (columns).
- `mov bx, ax`: Copy AX to BX (row index).
- `add bx, cx`: Add CX to BX (row index + column index).
- `mov dx, 0`: Initialize DX (column index).
- `multiply_loop:`: Label for the loop to perform multiplication and summation.
- `mov si, dx`: Copy DX to SI (column index).
- `add si, cx`: Add CX to SI (row index + column index).
- `mov ax, [matrix1 + bx * 2]`: Load element from "matrix1".
- `mov bx, [matrix2 + si * 2]`: Load element from "matrix2".
- `imul bx`: Multiply and store result in AX.
- `add [result + dx * 2], ax`: Add result to the appropriate location in "result" matrix.
- `inc dx`: Increment column index.
- `cmp dx, 2`: Compare with 2 (matrix dimension).
- `jl multiply_loop`: Jump if less than 2 (loop to multiply next element).
- `inc bx`: Increment row index.
- `loop inner_loop`: Decrement CX and loop if not zero.
- `inc ax`: Increment AX (column index).
- `cmp ax, 2`: Compare with 2 (matrix dimension).
- `jl outer_loop`: Jump if less than 2 (loop to multiply next row).
- `print_loop:`: Label for the loop to print the result matrix.
- `mov ax, 0`: Initialize AX (row index).
- `mov cx, 2`: Set CX to 2 (loop counter for rows).
- `print_inner_loop:`: Label for the inner loop to print matrix elements.
- `mov bx, ax`: Copy AX to BX (row index).
- `add bx, dx`: Add DX to BX (row index + column index).
- `mov ax, [result + bx * 2]`: Load element from "result" matrix.
- `call print_number`: Call procedure to print the number.
- `inc dx`: Increment column index.
- `cmp dx, 2`: Compare with 2 (matrix dimension).
- `jl print_inner_loop`: Jump if less than 2 (loop to print next column).
- `call print_newline`: Print newline to separate rows.
- `inc ax`: Increment AX (row index).
- `loop print_loop`: Decrement CX and loop if not zero.
- `mov ah, 4Ch`: Set the exit code for DOS.
- `int 21h`: Call DOS interrupt to exit the program.
The above programs showcase a range of assembly language concepts and techniques, including basic arithmetic, loops, conditional jumps, printing, and procedures. Keep in mind that these are relatively simple examples for demonstration purposes, and real-world assembly programs may involve more complexity and optimizations.
Comments
Post a Comment