简体   繁体   中英

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

  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. Pardon the pun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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