Solved

convert CHAR to INT in assembly language

Posted on 2004-04-25
4
909 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:ransula
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:twobitadder
ID: 10914419
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.
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10914436
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?
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10916764
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
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 125 total points
ID: 10916868
Hmmm, looks like you only have signed 4-bit immediates, and no "SUB" command. Very barebones :-|

If you want to load -48, the easiest way should be:

AND R1,R1,#0
ADD R1,R1,#-12
ADD R1,R1,R1   ; -24
ADD R1,R1,R1   ; -48
(or use LD to load it directly).

For a multiplication with 10, the most efficient way should be (initial value is in R1):
ADD R1,R1,R1   ; R1 = R1 * 2
ADD R2,R1,R1   ; R2 = R1 * 4
ADD R2,R2,R2   ; R2 = R1 * 8
ADD R1,R2,R1   ; R1 = R1 * 2 + R1 * 8


0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question