flat assembler
Message board for the users of flat assembler.
Index
> Main > convert string to integer |
Author |
|
gavin 30 Jul 2008, 18:26
Example:
string db "54333",0 needs to be -> number dw 54333 How would I go about this? |
|||
30 Jul 2008, 18:26 |
|
mattst88 31 Jul 2008, 02:53
Madis731 wrote: Take "5", subtract 30h, multiply by 10 => you get 50; A bit of clarification: 0x30 is the ASCII code for '0', so '0' (that is, 0x30) + 5 will give you the ASCII code for the character '5'. _________________ My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions. Assembly Programmer's Journal |
|||
31 Jul 2008, 02:53 |
|
kandamun 31 Jul 2008, 05:49
Madis731 wrote: Take "5", subtract 30h, multiply by 10 => you get 50; Of course as any nicely written code it has a small bug. There will be one extra zero at the end of the result. I guess it is Code: init with 0, multiply by 10, add "5", subtract 30h => you get 5. or any other way to remove the extra multiply by 10. |
|||
31 Jul 2008, 05:49 |
|
edfed 31 Jul 2008, 15:55
str2num, will convert string to decimal value.
it will seek the first char of the string, then, the last char of the number, tehn, start with zero value, and make the basic n=a-'0' +n*10 after, it will point to the end of the number. esi is used as source pointer. it will separate multiple numbers, by soem symbols. it can be used to convert many little strings in many numbers. Code: str2num: @@: mov al,[esi] inc esi cmp al,' ' je @b @@: mov al,[esi] inc esi cmp al,' ' je @f cmp al,'.' je @f cmp al,',' je @f cmp al,0 jne @b @@: dec esi push esi dec esi xor ebx,ebx lea edx,[ebx+1] @@: movzx eax,byte[esi] dec esi cmp al,'-' je .neg cmp al,' ' je .end cmp al,',' je .end cmp al,'.' je .end sub al,'0' jl @f cmp al,9 jg @f imul eax,edx add ebx,eax imul edx,10 jmp @b @@: pop esi clc ret .neg: mov al,[esi] cmp al,' ' jne @b neg ebx .end: pop esi stc ret used as a snippet in fool code. as it: Code: .box: call str2num jnc @f mov [shell.box+box.x],ebx @@: call str2num jnc @f mov [shell.box+box.y],ebx @@: call str2num jnc @f or ebx,ebx je @f mov [shell.box+box.xl],ebx @@: call str2num jnc @f or ebx,ebx je @f mov [shell.box+box.yl],ebx @@: call str2num jnc @f mov [shell.box+box.c],ebx @@: ret |
|||
31 Jul 2008, 15:55 |
|
Madis731 31 Jul 2008, 19:37
Sorry, kandamun, the 'spoken code' was flawed, but the ASM-code itself is okay. It inits to 0, then multiplys by 10 and adds the first number.
|
|||
31 Jul 2008, 19:37 |
|
FrozenKnight 02 Aug 2008, 21:17
this is a snippet form some code i did a few years back it coverts a floating point string to a floating point value. if you review it you will see one of my first major errors where i held the multiplier in ecx. Please note that this was my first attempt at writing code using the FPU.
note that this code doesn't do any checking if the number is valid or not as this was meant to be part of a subclassed dialog and the checking was done in a different area. Code: proc flttovalue, string:DWORD, value:DWORD;, decimal:DWORD local temp:DWORD, cword:WORD, multiplyer:TBYTE push ebx push ecx mov ebx, [string] finit ;fstcw [cword] ;mov ax, [cword] ;or ax, 00300h ;and ax, 0F3FFh ;mov [cword], ax ;fldcw [cword] ;mov [temp], 0 ;fild [temp] fldz xor eax, eax mov eax, 1 cmp BYTE [SS:ebx], '-' jnz @f mov eax, -1 inc ebx @@: push eax ;fldz .ipart: cmp BYTE [SS:ebx], '.' jz .fpart0 cmp BYTE [SS:ebx], 0 jz .fin ;mov [temp], 10 ;fild [temp] fimul [const10] xor eax, eax mov al, BYTE [SS:ebx] sub al, '0' mov [temp], eax fild [temp] faddp st1, st0 inc ebx jmp .ipart .fpart0: inc ebx lea ecx, [multiplyer] fld1 fstp tbyte [ecx] .fpart1: cmp BYTE [SS:ebx], '.' jz .fin cmp BYTE [SS:ebx], 0 jz .fin xor eax, eax mov al, BYTE [SS:ebx] sub al, '0' mov [temp], eax ;fild [temp] ;mov eax, 10 ;<==== error here ;mul ecx ;mov ecx, eax ;mov [temp], ecx ;mov [temp], 10 fld TBYTE [ecx] fidiv [const10] fstp TBYTE [ecx] fld TBYTE [ecx] fild [temp] fmulp st1, st0 faddp st1, st0 inc ebx jmp .fpart1 .fin: ;fldpi ;test pop eax mov [temp], eax fild [temp] fmulp st1, st0 ;fstcw [cword] ;mov ax, [cword] ;mov cx, WORD [iFloatType] ;shl cx, 8 ;or cx, 0F0FFh ;and ax, cx ;mov [cword], ax ;fldcw [cword] mov eax, [value] mov edx, [iFloatType] cmp edx, const80 jnz @f ;fldpi ;test fstp TBYTE [eax] @@: cmp edx, const64 jnz @f fstp QWORD [eax] @@: cmp edx, const32 jnz @f fstp DWORD [eax] @@: mov eax, esi pop ecx pop ebx ret endp |
|||
02 Aug 2008, 21:17 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.