如何接受具有大(多位数)数字的输入 [英] how to accept input with a large (multi-digit) number

查看:127
本文介绍了如何接受具有大(多位数)数字的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何接受大量输入,然后进行比较

How to accept input with a large number then compare it for example

mov ah,01h
int 21h

我想接受一个以上的字符,并将该数字移动到al,以通过输入用户示例(例如,用户想要键入32但单个字符为

i want to accept more than one characters and move that number to al to access the port in blinking an led by input of the user example the user wants to type 32 but single character was

mov bl,al
delay:
mov ctr,'0'
mov al,bl
mov cx,100
skip:
x:
mov al,00000000b
mov dx,378h
out dx,al
loop x

Z: mov al,bl
mov dx,378h
out dx,al 
loop z
inc ctr

Cmp ctr,'8'
Je exit
jmp skip
Exit :
Mov ah,4ch
int 21h 
End start

推荐答案

如果您需要接受多于一个数字的数字,则需要执行两个步骤:

If you need to accept numbers with more than one digit you need to execute two steps:

  1. 接受数字作为字符串,而不是单个字符.
  2. 使用过程将字符串转换为数字.

这是您接受字符串的方式:

This is how you accept a string:

mov ah, 0ah
mov dx, the_string
int 21h

变量the_string需要以下特定格式:

The variable the_string need this specific format:

the_string db 26         ;MAX NUMBER OF CHARACTERS ALLOWED (25).
           db ?          ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
           db 26 dup (?) ;CHARACTERS ENTERED BY USER (END WITH 0AH).

请注意三个部分:第一个字节表示允许的最大字符数(由于末尾的ENTER键而增加了一个),第二个字节将存储输入的字符串的长度,第三个字节是数组的数组字符(以ENTER键='0ah'结尾).

Notice the three sections: the first byte indicates the maximum number of chars allowed (plus one because of the ENTER key at the end), the second byte will store the length of the entered string, the third section is the array of chars (ended with ENTER key = '0ah').

一旦您接受了字符串,就必须使用已知算法将其转换为数字:处理字符,从右到左,将每个字符乘以10的幂.我们将这个过程称为"string2number".

Once you accepted the string, it is necessary to convert it into a number with a known algorithm: process the chars from right to left multiplying each by a power of 10. We will call this procedure "string2number".

过程"string2number"具有一个参数:指向字符串变量的寄存器SI(具有三个部分的一个).该编号返回到寄存器BX中.如果数字较小,则可能适合BL.在以后的程序中,此过程对您非常有用.

The procedure "string2number" takes one parameter: register SI pointing to the string variable (the one with the three sections). The number is returned in register BX. If the number is small it may fit into BL. This procedure will be very useful for you in your future programs.

接下来是您的代码,其中包含更改和结尾处的过程"string2number":

Next is your code with the changes and the procedure "string2number" at the end :

.model small
.stack 100h
.data                                           

str   db 4         ;MAX NUMBER OF CHARACTERS ALLOWED (3).
      db ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
      db 4 dup (?) ;CHARACTERS ENTERED BY USER (END WITH 0AH).
ctr   db 0      

.code
    mov  ax, @data
    mov  ds, ax   

;   mov ah,01h
;   int 21h
    mov ah, 0ah        ;◄■ CAPTURE STRING FROM KEYBOARD.
    mov dx, offset str ;◄■ VARIABLE TO STORE THE STRING.
    int 21h 

    mov si, offset str ;◄■ STRING TO CONVERT INTO NUMBER.
    call string2number ;◄■ NUMBER WILL RETURN IN BX.
    mov al, bl         ;◄■ COPY NUMBER INTO AL.    

    delay:
    mov ctr,'0'
    mov al,bl
    mov cx,100
    skip:
    x:
    mov al,00000000b
    mov dx,378h
    out dx,al
    loop x

    Z: mov al,bl
    mov dx,378h
    out dx,al 
    loop z
    inc ctr

    Cmp ctr,'8'
    Je exit
    jmp skip
    Exit :
    Mov ah,4ch
    int 21h 
;   End start    
;▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
;CONVERT STRING TO NUMBER IN BX.
;SI MUST ENTER POINTING TO THE STRING.
proc string2number
;MAKE SI TO POINT TO THE LEAST SIGNIFICANT DIGIT.
  inc  si ;POINTS TO THE NUMBER OF CHARACTERS ENTERED.
  mov  cl, [ si ] ;NUMBER OF CHARACTERS ENTERED.                                         
  mov  ch, 0 ;CLEAR CH, NOW CX==CL.
  add  si, cx ;NOW SI POINTS TO LEAST SIGNIFICANT DIGIT.
;CONVERT STRING.
  mov  bx, 0
  mov  bp, 1 ;MULTIPLE OF 10 TO MULTIPLY EVERY DIGIT.
repeat:
;CONVERT CHARACTER.                    
  mov  al, [ si ] ;CHARACTER TO PROCESS.
  sub  al, 48 ;CONVERT ASCII CHARACTER TO DIGIT.
  mov  ah, 0 ;CLEAR AH, NOW AX==AL.
  mul  bp ;AX*BP = DX:AX.
  add  bx,ax ;ADD RESULT TO BX. 
;INCREASE MULTIPLE OF 10 (1, 10, 100...).
  mov  ax, bp
  mov  bp, 10
  mul  bp ;AX*10 = DX:AX.
  mov  bp, ax ;NEW MULTIPLE OF 10.  
;CHECK IF WE HAVE FINISHED.
  dec  si ;NEXT DIGIT TO PROCESS.
  loop repeat ;COUNTER CX-1, IF NOT ZERO, REPEAT.

  ret 
endp    

这篇关于如何接受具有大(多位数)数字的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆