无法将ascii数字添加到数字8086程序集中

时间:2014-10-21 12:05:19

标签: assembly x86-16 tasm

我似乎碰壁了,找不到任何关于这个的例子。我需要将ascii字符转换为数字,即用户键入150 + 123,所以我必须读取ascii中的第一个数字,然后通过从每个数字减去0将其转换为dec,但这是问题所在。我不知道如何实际创建这些数字中的数字,因为用户可以输入1位数或5位。请忽略代码中的外来注释。谢谢

.model small
.stack 100h

.data

  prompt1 db "Please enter an action:  ", 10, 13, "$"
  prompt2 db "Answer:  ", 10, 13, "$"
  newline db 10, 13, '$'
  buffer db 255 ; Denotes the number of maximal symbols hope
  sizeread db 0 ; It will be available on how many characters scanned
  buffer_data db 255 dup (?) ; It will be hosted on-line scanned data to fill 255 characters $

.code
start:

  mov dx, @data ; 
  mov ds, dx    ; 

print_prompt:

  mov ah, 9     
  mov dx, offset prompt1 ; Messages placed in the DX register address
  int 21h ; welcome petraukimą

  ret

input:

  mov ah, 0Ah
  mov dx, offset buffer
  int 21h

  ret

number:

  xor bx,bx
  xor ax,ax
  mov al, 10   ;set al to 0 so i can MUL
  mov cx, 5    
  loopstart:   ;start the cycle
  cmp [buffer_data+bx], ' '    ;check if I reached a space
  je space     ; jump to space (the space func will read the + or - sign later on)
  mov bx, [bx+1] ; bx++
                        ;Now i got no idea how to go about this....

space:
end start

1 个答案:

答案 0 :(得分:2)

你需要将累加器乘以10(假设你的输入是十进制的,即如果十六进制乘以16):

    xor ax, ax   ; zero the accumulator
    mov cx, 10   ; cx = 10

input:
    movzx bx, <next input char from left to right>
    cmp bl, '\n' ; end of input
    je out
    sub bl, '0'  ; convert ascii char to numeric value
    mul cx       ; ax = ax*10, prepare for next digit (assuming result fits in 16 bits)
    add ax, bx   ; ax = ax + bx, add current digit
    jmp input

out:

达到'out'时,cx具有输入数字的数值。