8086汇编实现提示键盘输入第一个矩阵的行、第一个矩阵的列和第二个矩阵的行、第二个矩阵的列并输出两个矩阵相乘结果
时间: 2023-09-15 07:19:01 浏览: 115
下面是一个8086汇编程序实现提示键盘输入第一个矩阵的行、第一个矩阵的列和第二个矩阵的行、第二个矩阵的列并输出两个矩阵相乘结果的示例:
```assembly
.MODEL SMALL
.STACK 100H
.DATA
m1_row DB ?
m1_col DB ?
m2_row DB ?
m2_col DB ?
m1 DW 10 DUP(0)
m2 DW 10 DUP(0)
res DW 10 DUP(0)
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Prompt user for matrix 1 dimensions
MOV AH, 09H
LEA DX, m1_row_msg
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV m1_row, AL
MOV AH, 09H
LEA DX, m1_col_msg
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV m1_col, AL
; Prompt user for matrix 2 dimensions
MOV AH, 09H
LEA DX, m2_row_msg
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV m2_row, AL
MOV AH, 09H
LEA DX, m2_col_msg
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV m2_col, AL
; Prompt user for matrix 1 values
MOV CX, m1_row
MOV BX, m1_col
MOV DX, 0
LEA SI, m1
MOV AH, 09H
LEA DX, m1_prompt
m1_loop:
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV DL, AL
INC DX
MOV [SI], DL
ADD SI, 2
CMP DX, BX
JL m1_loop
LOOP m1_loop
; Prompt user for matrix 2 values
MOV CX, m2_row
MOV BX, m2_col
MOV DX, 0
LEA SI, m2
MOV AH, 09H
LEA DX, m2_prompt
m2_loop:
INT 21H
MOV AH, 01H
INT 21H
SUB AL, 30H
MOV DL, AL
INC DX
MOV [SI], DL
ADD SI, 2
CMP DX, BX
JL m2_loop
LOOP m2_loop
; Multiply matrices
XOR AX, AX
XOR BX, BX
XOR CX, CX
XOR SI, SI
LEA DI, res
outer_loop:
MOV CX, m1_col
inner_loop:
MOV AX, [m1+SI]
MOV BX, [m2+CX]
MUL BX
ADD [DI], AX
ADD DI, 2
INC CX
LOOP inner_loop
ADD SI, 2
MOV DI, res
ADD DI, SI
CMP SI, m1_col*2
JNE outer_loop
; Print result matrix
MOV CX, m1_row
MOV BX, m2_col
MOV DX, 0
LEA SI, res
MOV AH, 09H
LEA DX, res_prompt
res_loop:
INT 21H
MOV AX, [SI]
ADD SI, 2
CALL print_hex_word
DEC BX
JNZ res_loop
MOV AH, 02H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
LOOP res_loop
; Exit program
MOV AH, 4CH
INT 21H
print_hex_word PROC
PUSH AX
PUSH CX
PUSH DX
MOV CX, 4
MOV BX, 1000H
print_hex_digit:
XOR DX, DX
DIV BX
CMP DL, 0AH
JGE hex_letter
ADD DL, 30H
JMP print_hex_continue
hex_letter:
ADD DL, 37H
print_hex_continue:
MOV AH, 02H
MOV DL, DL
INT 21H
LOOP print_hex_digit
POP DX
POP CX
POP AX
RET
print_hex_word ENDP
m1_row_msg DB 'Enter matrix 1 row: $'
m1_col_msg DB 'Enter matrix 1 column: $'
m2_row_msg DB 'Enter matrix 2 row: $'
m2_col_msg DB 'Enter matrix 2 column: $'
m1_prompt DB 'Enter matrix 1 values: $'
m2_prompt DB 'Enter matrix 2 values: $'
res_prompt DB 'Result matrix: $'
END MAIN
```
这个程序会提示用户输入两个矩阵的维度和值,然后计算两个矩阵的乘积,并输出结果矩阵。程序使用了一些8086汇编的指令和语法,如果你不熟悉这些内容,可以参考一些教程和文档进行学习。
阅读全文