0

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?

user1869703
  • 53
  • 3
  • 7
  • 1
    Subtract '0'? (Which, as you already know, is `0x30` in hex.) – Jongware Apr 06 '14 at 21:33
  • doesnt work because 3933 in hex is way larger than 93. So I have to convert it so that it gives the value 93. – user1869703 Apr 06 '14 at 21:45
  • 1
    But every single *digit* is entered in ASCII. So you have to subtract 0x30 from every digit as well. (You'll end up with a series of 'pure' binary digits, which you then need to combine into a single binary number. Basic arithmetic, I'm afraid.) – Jongware Apr 06 '14 at 21:47
  • Yes, I know that 3933 split up as 39 33 can each be subtracted by 30 to get 93 but that isn't what i'm asking for. Say I input 93 and that is saved as 3933 but I want it to give me the hex value of "5D" – user1869703 Apr 06 '14 at 22:02
  • Um, you lost me there. If you have a list of correct (binary) values, you can combine them in the *usual* way to get a total of '93' (which is your '5D'). – Jongware Apr 06 '14 at 22:05
  • So in easy68k it'll save as a 3933 but it is all in hex. if i only subtract then itll give me 93 in hex which is 147 in decimal. – user1869703 Apr 06 '14 at 22:08
  • Losing you even more. I'm going to log off until you clarify your question. Are you confusing "binary" with "hex"? Your last statement suggests you need to, or *think* you need to, enter and/or convert from *hex* input. – Jongware Apr 06 '14 at 23:55
  • In easy68k The values are stored as Hex. So if I have 3933 and I only subtract 30 then it will remain as a hex. So I will have to do a subroutine that will do more than just subtract because I need it as exactly "5D". It will not convert the "93" by just subtracting because sure i can subtract 0x30 but itll only leave me with more hex values. – user1869703 Apr 07 '14 at 07:07
  • As has already been mentioned, you need to subtract `0x30` from each byte, then you combine these by multiplying each byte with increasing powers of 10 and adding the results. – Michael Apr 07 '14 at 08:00
  • Oh. I realized i was multiplying the first one by ten as well. thats why it wasnt working. – user1869703 Apr 08 '14 at 01:30
  • Not sure if i can help, but first seperate a half byte (Nibble) and if the value is greater then ASCII "9", then simple add the value of 7 for to convert to an ASCII of A - F, if you want hexadecimal output. – Dirk Wolfgang Glomp Apr 08 '14 at 19:44

2 Answers2

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:
  ...
turboscrew
  • 676
  • 4
  • 13
0

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.

unwind
  • 391,730
  • 64
  • 469
  • 606