Reputation: 3
I am trying to convert texto to number. Exemple I digit "321" and I need to conver it to number: 321.
First i copy to text
f: mov al,es:[bx]
mov texto[di],al
inc bx
inc bx
inc di
loop f
Then I want to convert to number.
xor di, di
mov cl, 10
li:
mov al,texto[di]
mov numero[di],al
sub al, 48
mul cl
xor al, al
inc di
loop li
The first code I think is ok, but the second is wrong. I don't get it.
Upvotes: 0
Views: 203
Reputation: 46970
It can help to code in a HHL before assembly. If the algorithm is at all complex, run it in the HLL so that you're sure it works before committing to assembly code. For example, this:
char text[] = "321";
int n_chars = 3;
int val = 0;
int i = 0;
next:
val = 10 * val + text[i++] - '0';
if (--n_chars) goto next;
In MASM,
mov bl, 10 ; constant multiplier
mov cl, 3 ; n_chars = 3
xor ax, ax ; val = 0
xor si, si ; i = 0
next:
mul bl ; val = 10 * val
add ax, text[si] ; val = val + text[i]
sub ax, '0' ; val = val - '0'
inc si ; i++
loop next ; if (--nChars) goto next
Upvotes: 2
Reputation: 11
you just want to convert "321" to 321?
You have to sub 30h to each value and add it to the final number. example:
.8086
.model small
.stack 2048
DATA segment para public 'data'
number db '321$'
final dw 0
DATA ends
CODE segment para public 'code'
assume CS:CODE, DS:DATA
main proc
mov ax, DATA
mov ds, ax
mov si,0
mov cx,10
xor bx,bx
cicle:
mov ax,final
mul cx
mov final,ax
mov bl,number[si]
sub bl,30h
add final,bx
inc si
cmp number[si],'$'
jne cicle
MOV AH,4ch
INT 21h
MAIN endp
CODE ends
END MAIN
I know it is not fully functional and you should expect some bugs.
EDIT: I just wanted to give an example so he could understand. Here is a full functional code. As long as the digit are before '$' it will work perfectly.
Upvotes: 1