汇编程序:1、编程计算S=1+3+5+......+99,并以十六进制形式显示S的值

2024-12-20 15:15:44
推荐回答(2个)
回答1:

;1+3+....+99=(1+99)*50/2=9C4
CODE SEGMENT
ASSUME CS:CODE
START: MOV CX,99
MOV AX,0
LOP: ADD AX,CX
SUB CX,2
JNS LOP
MOV BX,AX
CALL DISPLAY
MOV AH,4CH
INT 21H
;入口参数:BX
;功能:显示字数据
DISPLAY PROC
PUSH CX
MOV CH,4
MOV CL,4
LOP1: ROL BX,CL
MOV DL,BL
AND DL,0FH
ADD DL,30H
CMP DL,3AH
JB NEXT
ADD DL,7
NEXT: MOV AH,2
INT 21H
DEC CH
CMP CH,0
JNZ LOP1
POP CX
RET
DISPLAY ENDP
CODE ENDS
END START

回答2:

.386
STACK SEGMENT USE16 STACK
DB 2000 DUP(0)
STACK ENDS

DATA SEGMENT USE16
NUM DD 99
OUTPUT DB 0DH,0AH,'SUM 0F 1 .. 8000 IS : '
RESULT DB 16 DUP (' '),'$'
DATA ENDS

CODE SEGMENT USE16
ASSUME CS:CODE,DS:DATA,SS:STACK
START:
MOV AX, DATA
MOV DS, AX
MOV EBX, NUM
PUSH EBX
CALL SUM_P ;计算1....8000的和 结果在EAX中
CALL F2T10 ; (EAX) 转化为十进制可显示字符放到 result 缓冲区中
LEA DX, OUTPUT
MOV AH, 9
INT 21H
EXIT: MOV AH,4CH
INT 21H

SUM_P PROC
MOV BP,SP
MOV EBX, [BP+2]
MOV EAX,0
LP: ADD EAX,EBX
DEC EBX
cmp EbX, 0
JNZ LP
RET 4
SUM_P ENDP

; 将 (EAX) 中的内容,转换成 十进制数串
F2T10 PROC
MOV EBX, 10
LEA SI, RESULT+15
L1: MOV EDX, 0
DIV EBX
ADD DL,30H
MOV [SI],DL
DEC SI
CMP EAX, 0
JZ F_EXIT
JMP L1
F_EXIT:
RET
F2T10 ENDP

CODE ENDS
END START