简体   繁体   English

如何在MC68k中将ASCII码转换为二进制

[英]How to convert ascii number to binary in MC68k

I have to write a program that requires 20 user inputted numbers from 0-100 to find the average of the numbers and categorize them as failing or passing but it saves the input as ascii in memory and I have to change it from ascii to binary. 我必须编写一个程序,要求20个用户输入的0到100之间的数字来查找这些数字的平均值并将其分类为失败或通过,但是它将输入ascii保存在内存中,并且我必须将其从ascii更改为二进制。 I know that ascii numbers are 30-39 in hex but I am unsure about how to implement it in MC68K like if i input 93 as a number then it would be saved as 3933 but how do I convert it to binary? 我知道ascii的数字是十六进制的30-39,但是我不确定如何在MC68K中实现它,例如如果我输入93作为数字,那么它将保存为3933,但是如何将其转换为二进制?

  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:
  ...

Here's how I would do it: 这是我的处理方式:

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

Note: the above is untested assembly code for a processor I haven't touched in ... quite a while. 注意:以上是未经测试的处理器的汇编代码,已经有一段时间了。 Hopefully it can at least be inspirational. 希望它至少可以鼓舞人心。 Back in the day, of course nobody would have dared use muls here, but it's instructional. 过去,当然没有人敢在这里使用muls ,但这是指导性的。 Pardon the pun. 双关语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM