Solved

convert CHAR to INT in assembly language

Posted on 2004-04-25
4
898 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to implement a communication protocol 8 520
Error opening hao_sem 16 305
z390 Assembly 5 519
Application Trololo: "Do you want to GPF?" 4 518
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now