x86 - converting a vector of chars to int in assembly -


i'm trying following, i'm having trouble, , code find on web transforming strings number (basicly atoi), need different, e.g:

num1 db '60','30' num2 db '2', '3' num3 db '*', '*' 

basicly need transform chars in vector numbers (separately), can operation marked num3 num1 , num2 operators, example, i'll use function multiplies 2 numbers. tried was:

mov ax, dados mov ds, ax mov cx, 2 cycle:           cmp num3[si], 2fh     je divisao     cmp num3[si], 2ah     je multiplica     cmp num3[si], 2bh     je soma     cmp num3[si], 2dh     je subtracao     inc si     loop cycle      jmp fim 

the multiply function:

multiplica proc     push ax     mov ah, 0     sub num1[si], 48     mov al, num1[si]     sub num2[si], 48     imul num2[si]     mov dx, ax     pop ax     ret  multiplica endp 

i thought needed subtract 48 each position make correspondant number, guess there's more it. thanks. edit: did ajustments, found out it's multiplying first character, e.g: instead of 60*2, it's doing 6*2

yes, there more it. turn string byte, can use this

; input esi = null-terminated string ; output al = number str2byte:     push cx     mov cx, 0x0a00     xor al, al     .loop:         mul ch         mov cl, byte [esi]         inc esi         test cl, cl         jz .end          sub cl, 0x30         add al, cl         jmp loop     .end:     pop cx     ret 

... , multiplication

num1 db '60', 0 num2 db '2', 0 multiply:     mov esi, num1     call str2byte     mov ah, al      mov esi, num2     call str2byte      imul ah      ; result in ax     ret 

str2byte function requires esi contain null-terminated string allow numbers 100 or 255, , therefore use full range of byte.

edit:

if use more elements, better either use separate labels of them, e.g.

num1: db '60', 0 num2: db '4', 0 num3: db '7', 0 ... 

... or align them, smoothly through

numbers: ; of them aligned 4 bytes     db '60', 0, 0     db '4', 0, 0, 0     db '120', 0     ...  iterate:     mov esi, numbers     .loop:        ; something, multiplying         add esi, 4 ; increment 4 bytes = 1 number        jmp .loop 

edit 2:

however, elegant way through kind of strings start you've ended. means, can use chain null-terminated string in loop.

numbers:     db '60', 0     db '4', 0     db '120', 0     ...     db '13', 0     db 0  iterate:     mov esi, numbers     .loop:         ; something, let esi pointed @ beginning of every new string         cmp byte [esi], 0x0 ; 2 zeroes can mean end of strings / file         jnz .loop ; again 

please note

db 60 

takes single byte, while

db '60' 

takes 2 bytes: 1 '6' (0x36), , 1 '0' (0x30)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -