flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2 |
Author |
|
bitRAKE 19 Dec 2008, 06:25
Did some very minor testing - appears to work...
Code: BIGINT.itoa: ; rsi = BIGINT pointer ; rcx = qword limbs in big integer ; rdi = string buffer mov rbp,5*5*5*5*5*5*5*5*5 * 5*5*5*5*5*5*5*5*5 * 5*5*5*5*5*5*5*5*5 jmp .3 .1: xor edx,edx xor ebx,ebx push rcx .0: mov rax,[rsi+rcx*8] div rbp shrd rbx,rax,27+10 shrd rax,rbx,27 mov [rsi+rcx*8],rax dec ecx jns .0 shl rbx,10 shr rbx,27+10 ; N mod 10^27 = RBP * RBX + RDX mov rax,rbx mov rbx,rdx mul rbp add rax,rbx adc rdx,0 ; extract the 27 digits... pop rcx .2: cmp qword [rsi+rcx*8],0 jne .1 .3: dec ecx jns .2 retn |
|||
![]() |
|
edfed 20 Dec 2008, 01:17
finally, when i look deeper to my code, i see it is not so bad.
and is it very frequent to display more than 32bits values in decimal? Code: topofbuffer rd 1 .buffer rb 32 endofbuffer db 0 num: .dec: ; eax = number to convert mov esi,endofbuffer dec esi mov ebx,10 ; decimal base mov ch,' ' test eax,80000000h jne .positif neg eax ;eax = |eax| mov ch,'-' ;set the minus sign .positif: .next: cdq ; expand eax for the division ( xor edx,edx is ok) idiv ebx ; divide by 10 mov cl,dl ;extract one digit from the remainder, add cl,'0' mov [esi],cl ;store digit, high digit first dec esi ;increment digit rank cmp eax,0 ; do it until eax = 0 jne .next cmp ch,'-' ;is it a negative number? je @f mov ch,cl inc esi @@: mov [esi],ch mov [topofbuffer],esi ret decimal to ascii conversion of a 32 bits value. start the convertion with the least significant digit. the lsd is putted the char before the end of buffer (0) the buffer start should be returned for a printing ressource. the buffer size is 12 bytes (s)(1234567890)(,0) the value -123456 is equivalent of this after conversion: Code: topofbuffer dd xxxxxxxxxx>>>> v buffer db 0,0,0,0,0,0,0,'-','1','2','3','4','5','6' endofbuffer db 0 |
|||
![]() |
|
kempis 20 Dec 2008, 13:23
my code: convert to BCD with fbstp (Is it slow or fast?)
Code: dword_to_string: ;eax dword value ;edi pointer to string buffer push ebp mov ebp,esp sub esp,12 and esp,not 0xF mov dword[esp],eax mov dword[esp+4],0 fild qword[esp] mov ecx,5 fbstp [esp] ;store packed-BCD .loop1: sub ecx,1 mov al,[esp+ecx] test al,al jnz .br_loop1 test ecx,ecx jnz .loop1 mov byte[edi],'0' mov byte[edi+1],0 mov esp,ebp pop ebp ret .br_loop1: test al,0xF0 jnz .loop2 add al,'0' stosb test ecx,ecx jz .end_loop sub ecx,1 mov al,[esp+ecx] .loop2: mov ah,al shr al,4 and ah,0x0F add al,'0' add ah,'0' mov byte[edi],al mov byte[edi+1],ah add edi,2 test ecx,ecx jz .end_loop sub ecx,1 mov al,[esp+ecx] jmp .loop2 .end_loop: mov byte[edi],0 mov esp,ebp pop ebp ret |
|||
![]() |
|
revolution 20 Dec 2008, 13:32
Slow. FBSTP is a very un-optimal way to do it if your goal is speed.
|
|||
![]() |
|
revolution 20 Dec 2008, 13:42
I think I've posted this elsewhere but I can't find it just now so I'll post again.
Prints and unsigned dword in eax: Code: push -1 mov ebx,0cccccccdh .a: mov ecx,eax mul ebx and edx,-8 mov eax,edx sub ecx,edx shr edx,2 sub ecx,edx push ecx shr eax,3 jnz .a pop eax .b: add al,'0' ;;;put your print/store function here. Example only shown in next line stosb ;example only! pop eax test al,10h jz .b |
|||
![]() |
|
r22 21 Dec 2008, 03:54
Funny how this type of stuff is so overly complex on an x86. Even the divide by '10' method.
I used to program at an insurance company and their IBM mainframe (I think zOS s/370) had built in numeric formatting. In ASM it was the ED opcode and you'd use EPDIC character mask like "$,$$$,ZZZ.ZZ". An old school strictly business oriented processor would have that type of thing as part of the architecture and the x87 FPU only went as far as BCD. I guess the 'strictly business oriented' part answers my own non-question. [If you read this far you won't be getting any return on your time ![]() |
|||
![]() |
|
revolution 21 Dec 2008, 04:03
r22 wrote: Funny how this type of stuff is so overly complex on an x86. Even the divide by '10' method. r22 wrote: An old school strictly business oriented processor would have that type of thing as part of the architecture and the x87 FPU only went as far as BCD. I guess the 'strictly business oriented' part answers my own non-question. r22 wrote: [If you read this far you won't be getting any return on your time |
|||
![]() |
|
bitRAKE 21 Dec 2008, 05:57
It would be nice to have a couple custom opcodes.
![]() [I don't get a return on any of my time and the expiration date is too fuzzy to bother me either, lol.] |
|||
![]() |
|
revolution 21 Dec 2008, 06:05
But all x86 CPUs already have one custom opcode. All you need to do is set up a memory area with your required set of actions that you need the CPU to follow and then you execute the special custom opcode to do it. BTW The custom opcode is called CALL.
|
|||
![]() |
|
bitRAKE 21 Dec 2008, 07:16
My special opcode is int3, lol.
|
|||
![]() |
|
tthsqe 06 Jul 2009, 10:01
Here is a very short program that displays a 64-bit register in any base.
Code: format PE64 GUI entry start include 'win64a.inc' section '.text' code readable executable start: base=11 hexnumber=0xffffffffffffffff lea rdi,[answer] mov rcx,base mov r10,hexnumber call ConvertFromHex invoke MessageBox,NULL,answer,caption,MB_OK invoke ExitProcess,0 ConvertFromHex: ; rcx: base ; r10: number to convert ; rdi: address to write string to cld mov rbp,rsp mov rax,1 xor rdx,rdx loop_push: mov rbx,rax push rbx mul rcx jo loop_pop cmp rax,r10 jb loop_push loop_pop: xor rdx,rdx pop rcx mov rax,r10 div rcx mov al,[digit_table+rax] stosb mov r10,rdx cmp rbp,rsp jne loop_pop mov al,0 stosb ret section '.data' data readable writeable answer rb 65 caption db 'Result:',0 digit_table db '0123456789abcdefghijklmnopqrstuvwxyz',0 section '.idata' import data readable writeable library kernel32,'KERNEL32.DLL',\ user32,'USER32.DLL' include 'api\kernel32.inc' include 'api\user32.inc' |
|||
![]() |
|
Azu 07 Jul 2009, 18:41
revolution wrote:
|
|||
![]() |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.