Link to home
Start Free TrialLog in
Avatar of ransula
ransula

asked on

convert CHAR to INT in assembly language

hi,
i'm trying to convert a character, say, '3' to integer so that i can print it. this is what i've written ( and it's not working):
this is a procedure or function and written in LC-3 assembly language.
------------------------
;asciibias = 48
;1SC => one's compliment
; conversion starts here
AND R1,R1,#0      ; clears R1
LD R1,asciibias      ; load char '0' into R1
NOT R1,R1                      ; 1SC of 48
ADD R1,R1,#1      ; 2SC of 48 (ie -48)
ADD R2,R0,R1      ; R2 = (R0-R1)

AND R3,R3,#10          ; R3 is a counter
    loop      AND R1,R1,#0
                ADD R1,R1,R1
      ADD R1,R1,#-1
      BRp loop    ; branch if R1 is positive

; R1 = (R1 *10)
ADD R2,R2,R1
      ;now store the integer
ST R2,Num1

;print the char as integer
AND R0,R0,#0            
LD R0,Num1      ; this is the first integer
TRAP x21
- - - - - - - - -- -  - --------------------

what i'm doing wrong cos it's not working. infact it causes other parts of my program not to work (they work without a call to this function. in other words, if i comment out this procedure, other parts work)

any ideas and comments are greatly welcome
thx
r
Avatar of twobitadder
twobitadder

if you load asciibias into R1 i can't see the point of the previous line to clear R1 (it gets overwritten).

I'm assuming R0 holds the parameter to convert.
<quote>
AND R3,R3,#10          ; R3 is a counter
    loop     AND R1,R1,#0
                ADD R1,R1,R1
     ADD R1,R1,#-1
     BRp loop    ; branch if R1 is positive
</quote>

the above code will never loop, here's comments:
    loop     AND R1,R1,#0   ; makes R1 = 0;
                ADD R1,R1,R1   ;adds 0 to 0   !!
          ADD R1,R1,#-1 ;   ;R1 = 0-1 = -1
     BRp loop  ; will never be positive, R1 will always be -1/

I think the bulk of your problem is this loop.


Again at the end I can't see the point of clearing the register before you load a value into it unless there's a difference in register sizes.
Once you've added the two's complement of the asciibias (ie. subtracted it) and the result of your number less asciibias  is in R2, shouldn't you just move it to R0 and perform the interrupt?

I might not be clear on what you're doing, are you just trying to take an ascii character(stored in R2) and print its numerical value out?
Hi ransula,
> AND R0,R0,#0          
> LD R0,Num1     ; this is the first integer
> TRAP x21

I'm a bit confused about what you are doing. Can't you simply say (assuming your number character is in R2):

SUB R0, R2, #48
TRAP x21

???
What is the number range for immeditiates?

Cheers,
Stefan
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial