Solved

convert CHAR to INT in assembly language

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASM problem 2 31 346
Random "Missing Operator In Expression" error 3 922
assembly flow 3 603
How to do these things in assembly !? 1 438
In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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