当我从用户那里获取一个字符并确定字节中最大连续数字时,如何找到一个字节中连续的最大连续数。我尝试了这段代码,但是它没有用,它打印的是所有数字而不是连续的。
.model small
.stack 100
.data
message db "Enter one charachter $"
NewLine DB 0DH,0AH, "$"
message db "Enter one charachter $"
NewLine DB 0DH,0AH, "$"
message2 db "The maximum number of consecutive ones is $"
.code
mov ax, @data
mov ds,ax
push ax
mov ah,09
lea dx,message
int 21h
pop ax
MOV AH,09
MOV DX,OFFSET NewLine
INT 21H
mov ah,1
int 21h
mov si,0
mov di,8
l1:
shl al,1
jnc no_inc_count
inc si
no_inc_count:
dec di
jnz l1
MOV AH,09h
MOV DX,OFFSET NewLine
INT 21H
push ax
mov ah,09
lea dx,message2
int 21h
pop ax
MOV AH,09
MOV DX,OFFSET NewLine
INT 21H
mov ah,2
add si,30h
mov dx,si
int 21h
mov ah,4ch
int 21h
end
答案 0 :(得分:0)
他的区块统计所有的:
mov si,0
mov di,8
l1:
shl al,1
jnc no_inc_count
inc si
no_inc_count:
dec di
jnz l1
SI
是" one" -counter,用于计算那些
DI
是"循环" -counter,用于计算(减少)班次。
如果想要存储最多需要添加变量的数量" max"和两段代码:
我希望以下代码中的注释更清晰一点:
.MODEL small
.STACK ; Default 1024
.DATA
message db "Enter one character $"
message2 db "The maximum number of consecutive ones is $"
NewLine db 0DH,0AH, "$"
max dw 0 ; Holds the maximum number of consecutive ones
.CODE
start: ; Entry point needed for END
mov ax, @data ; Let DS point to .DATA
mov ds,ax
mov ah,09h ; http://www.ctyme.com/intr/rb-2562.htm
lea dx,message
int 21h ; Call MS-DOS
mov ah, 01h ; http://www.ctyme.com/intr/rb-2552.htm
int 21h ; Call MS-DOS
mov si, 0 ; One-counter
mov di, 8 ; Loop-counter: Shift 8 bits
FOR: ; Loop 8 times
shl al, 1 ; shift one bit into carry flag (CF)
jnc IS_NULL ; jump if no "one"
IS_ONE: ; Code to increment one-counter and replace max
inc si ; one-counter++
cmp si, max ; si > max ?
jbe LOWER ; No - comparation has set CF or ZF
GREATER: ; Yes, si > max (comparation has cleared CF and ZF)
mov max, si ; Replace max by SI
LOWER:
jmp CONTFOR ; Decrement loop-counter and loop
IS_NULL:
mov si, 0 ; Reset one-counter
CONTFOR: ; Decrement loop-counter and continue looping
dec di
jnz FOR
mov ah, 09h ; http://www.ctyme.com/intr/rb-2562.htm
lea dx, NewLine
int 21h ; Call MS-DOS
mov ah, 09h ; http://www.ctyme.com/intr/rb-2562.htm
lea dx, message2
int 21h ; Call MS-DOS
add max, 30h ; To ASCII
mov ah, 2 ; http://www.ctyme.com/intr/rb-2554.htm
mov dx, max
int 21h ; Call MS-DOS
mov ah, 09h ; http://www.ctyme.com/intr/rb-2562.htm
lea dx, NewLine
int 21h ; Call MS-DOS
mov ax,4C00h ; AH=4Ch (exit), exitcode AL=0 (ok)
int 21h ; Call MS-DOS
END start ; END < entry point >
注意:
END
语句的参数。PUSH
和POP
。请改为使用.DATA
细分中的变量。MOV AH,09h
)或小写字母(mov ah,09
)字符,则无关紧要,但您应该遵循您选择的方法。