如何在MC68k中将ascii数转换为二进制数

时间:2014-04-06 21:13:44

标签: assembly easy68k

我必须编写一个程序,需要20个用户输入的数字,从0到100才能找到数字的平均值,并将它们归类为失败或传递,但它将输入保存为ascii在内存中,我必须从ascii更改它二进制。我知道ascii数字是十进制的30-39,但我不确定如何在MC68K中实现它,就像我输入93作为数字然后它将保存为3933但是如何将其转换为二进制?

2 个答案:

答案 0 :(得分:0)

  clear number
loop:
  get highest digit character not dealt with yet
  compare digit character to '0' ; that's character 0, not value 0
  blo done
  compare digit character to '9'
  bhi done
  multiply number by 0x0a ; 10 in decimal
  subtract 0x30 from the character
  add to number
  jmp loop
cone:
  ...

答案 1 :(得分:0)

我将如何做到这一点:

str_to_int:
    ; Converts a decimal signed 0-terminated string in a0 into an integer in d0.
    ; Stops on the first non-decimal character. Does not handle overflow.
    ; Trashes other registers with glee.
    moveq #0, d3
.signed:
    cmpi.b #'-',(a0)     ; Check for leading '-'.
    bne.s  .convert
    bchg   #0,d3
    addq.l #1,a0
    bra.s  .signed
.convert:
    moveq.l #0,d0
    moveq.l #0,d1
.digit:
    move.v (a0)+,d1
    beq.s  .done
    subi.b #'0',d1       ; Convert to integer.
    bmi.s  .done         ; If < 0, digit wasn't valid.
    cmpi.b #'9'-'0',d1
    bgt.s  .done         ; If larger than 9, done.
    muls.l #10,d0
    add.l  d1,d0
    bra.s  .digit
.done:
    tst.b  d3
    bne.s  .signed
    rts
.signed:
    neg.l  d0
    rts

注意:以上是未经测试的处理器的未经测试的汇编代码,我还没有触及......很长一段时间。希望它至少可以鼓舞人心。当天回来,当然没有人敢在这里使用muls,但这是教学的。原谅双关语。