代码:
DATA SEGMENT
SUM DW 0
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START:
MOV AX, DATA
MOV DS, AX
MOV CX, 100
MOV AX, 0
MOV BX, 1
SUM_LOOP:
ADD AX, BX
INC BX
LOOP SUM_LOOP
MOV SUM, AX
MOV AX, 4C00H
INT 21H
CODE ENDS
END START
为什么会出现这样的错误?如何改变
将数字 1 到 100 相加得出 50 * (1 + 100),即 5050 (13BAh)。所以你可以写
mov ax, 50 * (1 + 100)
... mov cx, 100
xor ax, ax
SUM_LOOP:
add ax, cx
loop SUM_LOOP
总有一天您需要打印程序的结果,那么为什么不从今天开始呢?
; Code to display AX in decimal
mov di, 10 ; CONST divider
push di ; Sentinel
.a:
xor dx, dx
div di ; DX:AX / DI
push dx ; Push remainder [0,9]
test ax, ax
jnz .a
pop dx
.b:
add dl "0" ; From [0,9] to ["0","9"]
mov ah, 02h ; DOS.PrintCharacter
int 21h
pop dx
cmp dx, di
jb .b ; Continu until we popped the sentinel