little help Convertion of ASCII to it's decimal representation. Assembly code

Status
Not open for further replies.

hamradio

Newbie level 6
Joined
Jul 17, 2011
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,376
Hello guys, I have a task for my study. The task is simple but for beginner in assembly it's not so easy.
The task is consist of writing a program for converting the ASCII numbers from 0 to 9 and the letters from A to F to it's decimal representation. And if the input value don't match these ranges to turn on a LED. Simple and easy sounds.
I reached a point where the code is working but the last check for the LED I can't explain to myself what is the right way.
So if you have time please have a look any help is appreciated.

Code:
;=============================
; DEFINITIONS
;=============================

#include p16f84a.inc                ; Include register definition file

;================================
; VARIABLES
;================================
input 	EQU H'21'
result	EQU H'23'
;================================
; RESET and INTERRUPT VECTORS
;================================
 	  reset ORG 0000
      goto  Start
;================================
; CODE SEGMENT
;================================
	  ORG 0x10
Start
      clrf result
	  movfw input  ;load the input to W	  	
	  addlw -D'10'  ;check if the character code is it from A to F
	  addlw -H'30'  ;substract the base 
	  btfsc STATUS,C ; check the character
	  goto InputAF	; go to subroutine if the character code is in range A to F  
	  addlw D'10'  ; revert back to original character code
	  movwf result ; subtracted and saved.
	  goto Terminate
	  
InputAF
	  addlw D'3'  ; fill the number to the correct value
	  movwf result
	  goto Terminate
;===============================
Terminate      
	  END
 

Hi,

Think this is what you want to do, first set up the Ports and then when you enter your routine you can turn on a port pin as shown.

Note that I have removed the RESET from the ORG 0x00 line, the assembler shows that as invalid.

Also the movfw input is not correct, should be as shown. ( are you using a recent version of mplab ? )

Assume you are using MPLABS SIM to F7/single step though your code using a Watch window so you can see the effect of each instruction ?

While your logic does work on 0 -9 and A-F, what about any other ASCII characters eg G H e f g h ?
 

Attachments

  • 000021.jpg
    78 KB · Views: 120

I'm using MPLAB v8.92 but the code works, the main idea is to filter somehow any other characters than 0-9 and A-F. I need a hint for validating this input values and omit everything different than 0-9 and A-F and turn on LED if the input doesn't match these two ranges (0-9 A-F).
Thank you for your response.
 


Hi,

If you look at a ascii/hex/decimal chart you will see you that Ascii 0 - 9 is not followed by A - F, so you have two distinct ranges to deal with.

My take would be to work with the Ascii numbers direct and use 'work' registers rather than W all the time.
As shown earlier just enclose the character to denote an Ascii value movlw 'A'
Is the received character equal or greater than Ascii A, is the character equal or less than F ... using the STATUS register to check the result of the comparisons

Code:
	movlw	'E' ; incoming character
	movwf	input
	movlw	'F'        ; compare against
	subwf	input
  etc
 
Reactions: hamradio

    V

    Points: 2
    Helpful Answer Positive Rating

    hamradio

    Points: 2
    Helpful Answer Positive Rating
I managed to finish the code. Thank you. After optimization I'll re post it.
 

RESOLVED Convertion of ASCII to it's decimal representation. Assembly code

I'm sharing my working code. if its helpful for someone.

Code:
;====================================================================
; Name: asciitodec
; Title: ASCII to decimal convertion A to F and 0 to 9 
; Created:   Thursday April 10 2014
; Processor: PIC16F84A
; Compiler:  MPASM (MPLAB)
;====================================================================
;====================================================================
; DEFINITIONS
;====================================================================
#include p16f84a.inc              ; Include register definition file
;====================================================================
; VARIABLES
;====================================================================
input 	EQU H'21'
result	EQU H'23'
;====================================================================
; RESET and INTERRUPT VECTORS
;====================================================================
reset 	        ORG 0x00
goto  	Start
;====================================================================
; CODE SEGMENT
;====================================================================
		ORG 	0x10
Start
		banksel TRISB			; select bank1
		clrf	   TRISB			; make portB digital output
		banksel PORTB			; select bank0
		clrf	   PORTB			; ensure low on portB pins
 		clrf 	   result
Chk_1						; #Check routine range A'0-9'#
		movlw 	H'30'			; load low range limit A'0'
	  	subwf 	input			; substract
	  	btfss 	        STATUS,C		; check the input >= A'0'
	  	goto	        LEDerr		; >>>>> Turn On LED 
      	        movfw 	input			; return the input
	  	addlw 	H'30'			; recover the input
	  	movwf 	input			; write back input
	  	movlw 	H'3A'			; load up range limit A'9' 
	  	subwf 	input			; substract
	  	btfsc  	STATUS,C		; check the input <= A'9'
	  	goto 	        Chk_2		; The input is something else 
	  	
		movfw 	input			; return the input
	  	addlw 	H'3A'			; recover the input
		addlw 	-H'30'		; substract base
		movwf 	result		; write the result
		goto	       Terminate		; Terminate the programme

Chk_2 						; #Range check routine A'A-F'#
		movfw 	input			; return the input
	  	addlw 	H'3A'			; recover the input
	  	movwf 	input			; write back input
	  	movlw 	H'41'			; load low range limit A'A'
	  	subwf 	input			; substract
	  	btfss  	STATUS,C		; check the input >= A'A'
	  	goto	        LEDerr		; >>>>> Turn On LED 
      	        movfw 	input			; return the input
	  	addlw 	H'41'			; recover the input
	  	movwf 	input			; write back input
	  	movlw 	H'47'			; load up range limit A'F' 
	  	subwf 	input			; substract
	  	btfsc 	        STATUS,C		; check the input is <= A'F'
	  	goto	        LEDerr		; >>>>> Turn On LED 
	  	
		movfw 	input			; return the input
	  	addlw 	H'47'			; recover the input
		addlw 	-H'37'
		movwf	result
		goto	       Terminate		; Terminate the programme
LEDerr
		bsf		PORTB,0		; >>>>> Turn On LED
Terminate      
	  	END
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…