Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert CHAR to INT in assembly language

Posted on 2004-04-25
4
Medium Priority
?
925 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Take a look at these 6 Outlook Email management tools which can augment the working and performance of Microsoft Outlook to give you a more rewarding emailing experience.
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 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