Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] need to store ascii digits into 2 byte number

Status
Not open for further replies.

salamah99

Newbie level 5
Joined
Jul 24, 2023
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
81
I have an example like :
movlw D'1'
movwf digit1
movlw D'2'
movwf digit2
movlw D'3'
movwf digit3
movlw D'4'
movwf digit4
movlw D'5'
movwf digit5

I want a function that converts these ascii digits to a decimal two bytes number which should be 12345, note that this is only an example, I want to be able to store any number less than or equal to 65535 , i am trying to make it done but nothing works
 

I want to be able to store any number less than or equal to 65535 ,

Equals FFFF in hex. Did you set aside four bytes to do this?

Does the system read it as signed or unsigned? (If you leave it unspecified then the default format is assumed to be signed arithmetic.)
 

Equals FFFF in hex. Did you set aside four bytes to do this?

Does the system read it as signed or unsigned? (If you leave it unspecified then the default format is assumed to be signed arithmetic.)
it's unsigned,
I defined variable
RESULT RES 2 to reserve 2 bytes
actually I want to read 2 number (2 bytes each), and do calculations over them like addition and division, all of them unsigned
 

Hi,

basically you just have to declare a "uint16" varaible.
* it is 2 bytes in size
* can keep integers form 0...65535

HEX, octal, binary, decimal, ASCII ..is just the way the number (in the variable) is represented (displayed).
.. and has nothing to do with storage, math...

Klaus
 

Hi,

basically you just have to declare a "uint16" varaible.
* it is 2 bytes in size
* can keep integers form 0...65535

HEX, octal, binary, decimal, ASCII ..is just the way the number (in the variable) is represented (displayed).
.. and has nothing to do with storage, math...

Klaus
I am writing an assembly code, not C, and I can't use C so this is the real matter.
 

Hi,

You need to explain:
* what´s the exact problem.. in detail
* what´s your exact microcontroller
* why you can´t use C

i am trying to make it done but nothing works
This is no information at all. What did you try (show us) and why did it not work (also show us) and also tell us what you expected to see.

***
If you have 5 registers (1 byte each) and there are the value of each digit (range 0..9).
* then the lower significant byte has a "multiplier of 1", i.e. "5" means indeed 5
* then the next byte has a "multiplier of 10", i.e."4" means indeed 40
* then the next byte has a "multiplier of 100", i.e."3" means indeed 300
and so on.
so the total value is 5 x 1 + 4 x 10 + 3 x 100 ....

Klaus
 

in ascii, the numbers 0 through 9 are hex 30 through 39
read in the ascii, treat it like a number, subtract 30
the remaining value is the number in binary.

assembler does not have types, so the information stored in a memory
is whatever you treat it as.
so you can treat the ascii you read in as a number, not a character represntation
 

Multiply with 10^n works but reversed double dabble is simpler, see post #3.
 

Hi,

You need to explain:
* what´s the exact problem.. in detail
* what´s your exact microcontroller
* why you can´t use C


This is no information at all. What did you try (show us) and why did it not work (also show us) and also tell us what you expected to see.

***
If you have 5 registers (1 byte each) and there are the value of each digit (range 0..9).
* then the lower significant byte has a "multiplier of 1", i.e. "5" means indeed 5
* then the next byte has a "multiplier of 10", i.e."4" means indeed 40
* then the next byte has a "multiplier of 100", i.e."3" means indeed 300
and so on.
so the total value is 5 x 1 + 4 x 10 + 3 x 100 ....

Klaus
I need to complete a task and task is to implement an assembly code
pic16F877A
I tried the approach you are talking about, but my code wasn't strong enough to be able to achieve my goal, it didn't convert the number in the right way, so i am looking for someone who can write a valid function
 

Hi,

don't expect anyone to write code for you unless you pay for it.
"Helping" here means: you write the code and we help you to correct it.

Klaus
 

Guys, I am setting the decimal digit into each variable, then I am trying to write code like follows:

SPECIAL_COMBINE


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
clrf SPECIAL_COMB_HIGH_BYTE
    clrf SPECIAL_COMB_LOW_BYTE
 
    movlw 0x27   
    movwf TEMP_HIGH_BYTE
    movlw 0x10
    movwf TEMP_LOW_BYTE
 
    movf DIGIT5, 0
    call MULTIPLY
    movf PRODUCT_HIGH_BYTE, 0
    addwf SPECIAL_COMB_HIGH_BYTE, 1
    movf PRODUCT_LOW_BYTE, 0
    addwf SPECIAL_COMB_LOW_BYTE, 1
 
    movlw 0x03
    movwf TEMP_HIGH_BYTE
    movlw 0xE8
    movwf TEMP_LOW_BYTE
 
    movf DIGIT4, 0
    call MULTIPLY
    movf PRODUCT_HIGH_BYTE, 0
    addwf SPECIAL_COMB_HIGH_BYTE, 1
    movf PRODUCT_LOW_BYTE, 0
    addwf SPECIAL_COMB_LOW_BYTE, 1
 
    movlw 0x00
    movwf TEMP_HIGH_BYTE
    movlw 0x64
    movwf TEMP_LOW_BYTE
 
    movf DIGIT3, 0
    call MULTIPLY
    movf PRODUCT_HIGH_BYTE, 0
    addwf SPECIAL_COMB_HIGH_BYTE, 1
    movf PRODUCT_LOW_BYTE, 0
    addwf SPECIAL_COMB_LOW_BYTE, 1
 
    movlw 0x00
    movwf TEMP_HIGH_BYTE
    movlw 0x0A
    movwf TEMP_LOW_BYTE
 
    movf DIGIT2, 0
    call MULTIPLY
    movf PRODUCT_HIGH_BYTE, 0
    addwf SPECIAL_COMB_HIGH_BYTE, 1
    movf PRODUCT_LOW_BYTE, 0
    addwf SPECIAL_COMB_LOW_BYTE, 1
 
    movf DIGIT1, 0
    addwf SPECIAL_COMB_LOW_BYTE, 1
 
    return
 
MULTIPLY
    clrf PRODUCT_HIGH_BYTE
    clrf PRODUCT_LOW_BYTE
 
    movf TEMP_HIGH_BYTE, 0
    movwf MULTIPLICAND_HIGH_BYTE
    movf TEMP_LOW_BYTE, 0
    movwf MULTIPLICAND_LOW_BYTE
 
    movf W_TEMP, 0
    movwf MULTIPLIER
 
    clrf BITCOUNT
    movlw 0x10
    movwf BITCOUNT
 
MULTIPLY_LOOP
 
    btfsc MULTIPLIER, 0
    ADD_MULTIPLICAND
    rrf MULTIPLIER, 1
 
  
    rlf MULTIPLICAND_LOW_BYTE, 1
    rlf MULTIPLICAND_HIGH_BYTE, 1
 
  
    decfsz BITCOUNT, 1
    goto MULTIPLY_LOOP
 
    return
 
ADD_MULTIPLICAND1
  
    movf MULTIPLICAND_LOW_BYTE, 0
    addwf PRODUCT_LOW_BYTE, 1
    btfsc STATUS, C
    incf PRODUCT_HIGH_BYTE, 1
 
    movf MULTIPLICAND_HIGH_BYTE, 0
    addwf PRODUCT_HIGH_BYTE, 1
 
    return



*note that each digit here contains the decimal value and whenever we combine the maximum number will not exceed 65535
this code doesn't show the right result. I need to combine the digits into a 2-bytes number

[ MODERATOR ACTION: Added syntax tags ]
 
Last edited by a moderator:

There are no comments. Crappy code has no comments.

What are you seeing? What're you expecting? Have you simulated this?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top