【问题标题】:cannot add up ascii digits into a number 8086 assembly无法将 ascii 数字加到数字 8086 程序集中
【发布时间】:2014-10-21 12:05:18
【问题描述】:

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

.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

【问题讨论】:

标签: assembly x86-16 tasm


【解决方案1】:

您需要将累加器乘以 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 具有输入数字的数值。

【讨论】:

  • 真的没必要用CX,可以一直用AX。此外,没有mul 10 指令,您应该使用movzx 而不是xor + mov 对。琐碎的 cmets 没有用,您应该显示行的更高含义。我们知道 xor cx, cxcx 归零,但我们不知道您为什么将其归零以及 cx 的用途。
  • 对。自从我使用 8086 程序集以来已经有一段时间了……已编辑。
猜你喜欢
  • 1970-01-01
  • 2016-04-15
  • 2012-05-04
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
相关资源
最近更新 更多