将用户输入放入CX

时间:2016-04-10 03:25:13

标签: assembly x86-16 tasm

首次尝试:我已输入预定义数据。当用户输入时,我的其他代码将无效。

.model small
.stack
.data
    message db 10,13,"Multiply 2 x 2 x 16 =","    ","$"
space db 10,13,"$"
.code
    mov ax,@data
    mov ds,ax



;2 x 2 = [4]--------------- 
    mov cx,2                ;cx = 2, 1, 0
    mov bx,0                ;bx = 0, 2, 4
again1:
    add bx,2                ;bx = 4 
        loop again1
;[4] x 16 = 64---------------   
    mov cx,bx               ;cx = 4, 3, 2, 1, 0                 
    mov bx,0                ;bx = 0,16,32,48,64
again2:
    add bx,16               ;bx = 64 -> ax(ah,al)
        loop again2
        mov ax,bx   

;64 = Print "6 and 4"------

    mov cx,65535            ;cx = 65,535 times (almost infinite # of loops)
    mov bl,0                ;bl =    0,         1,      2,      3,      4,      5, 6
                            ;al = 64(-10),54(-10),44(-10),34(-10),24(-10),14(-10), 4
again3: 
    sub al,10
    cmp al,10               ;al(4) is less than 10
    jl next                 ;go to "NEXT"
        inc bl
            loop again3

next:                       ;NEXT
    mov ah,09
    lea dx,message
    int 21h

    add al,30h              ;al = 4 + 30 = "34" when printed is equals to "4"
    mov cl,al

    add bl,31h              ;bl =  6 + 30 = "36" when printed is equals to "6"
    mov dl,bl               ;print 6
    mov ah,02
    int 21h

    mov dl,cl               ;print 4
    mov ah,02
    int 21h

    mov ah,09
    lea dx,space
    int 21h

        ;2x2x16 =64 

    mov ah,4ch
    int 21h
end

以下是用户输入的内容:

.model small
.stack
.data
    message db 10,13,"The Answer is ...","  ","$"
    space db 10,13,"$"
.code
    mov ax,@data
    mov ds,ax


    mov ah,01       ;user input 1 -> al -> cl******
    int 21h
    mov cl,al

;? x ? = [?]--------------- 
    mov cx,?                ;cx = ?, 0
    mov bx,0                ;bx = 0, ?[+bx]

        mov ah,01       ;user input 2 -> al -> bl******
        int 21h
        mov bl,al
again1:
    add bx,?                ;bx = last count of bx 
        loop again1
;[?] x ? = ??---------------    
    mov cx,bx               ;cx = ?, 0                  
    mov bx,0                ;bx = 0, ?[+bx]

    mov ah,01       ;user input 3 -> al -> bl******
    int 21h
    mov cl,al
again2:
    add bx,?                ;bx = TOTAL# -> ax(ah,al)
    loop again2
     mov ax,bx  

;TOTAL# = Print "#1stDigit and #2ndDigit"------2x2x5=20

    mov cx,65535            ;cx = 65,535 times (almost infinite # of loops)
    mov bl,0                ;bl =       0,         1,     2,     3,     4,     5, 6     <-COUNT#
                        ;al = TOTAL#(-10),#(-10),#(-10),#(-10),#(-10),#(-10), ......FINAL#
again3: 
    sub al,10
    cmp al,10               ;al(FINAL#) is less than 10
    jl next                 ;go to "NEXT"
        inc bl
            loop again3

next:                       ;NEXT
    mov ah,09
    lea dx,message
    int 21h

    add al,30h              ;al = FINAL# + 30 =  when printed is equals to "FINAL# real value"
    mov cl,al

    add bl,31h              ;bl = COUNT# + 30 = when printed is equals to "COUNT# real value"
    mov dl,bl               ;print 1st digit
    mov ah,02
    int 21h

    mov dl,cl               ;print 2nd digit
    mov ah,02
    int 21h

    mov ah,09
    lea dx,space
    int 21h

        ;2x2x16 =64 

    mov ah,4ch
    int 21h
end

它只是给了我错误的答案:/我对TASM很新,它杀了我

基本上,我不知道如何将USER INPUT放在cx中。我只需要使用mov,ah 01h,这意味着用户只能输入1位数。

PLUS,我还有一个问题,如果最终答案是3位#。

示例,2 x 2 x 16 = 64(安全,仅限2位数)

但是5 x 3 x 9 = 135(当我运行程序时,只给我5个) 我没有得到3位数答案的逻辑。

1 个答案:

答案 0 :(得分:1)

mov ah,01       ;user input 1 -> al -> cl******
int 21h
mov cl,al

由于输入功能为您提供了字符,您仍然需要通过减去48来将其转换为可用的数字。 您还需要在CH寄存器中添加零,因为您将使用整个CX作为计数器。

mov ah,01       ;user input 1 -> al -> cl******
int 21h
sub al,48
mov cl,al
mov ch,0

关于3位数答案的逻辑:

你应该用一系列2个这样的结构替换 again3 结构。第一个将重复减去 100 ,第二个将重复减去 10 。在两者之间,您将结果保存到不同的寄存器或变量。