Recent Posts

Pages: 1 ... 6 7 [8] 9 10
71
Programming with NASM / Re: Printing of floating point values
« Last post by andyz74 on October 26, 2023, 07:12:13 PM »
You can separate the integer part from the fractional part with cvttss2si or cvttsd2si, and subtract the original values to get the fractional part (remember to obtain the absolute values). Then, for integer part, you can obtain the # of decimal algarisms using log10, and divide the integer value by 10, in sequence, util you get a zero quotient - getting the actual algarisms... For the fractional part, you just need to multiply them by 10 (until get 0.0 or the # of algarisms you want).

There are faster ways...

Ah, many thanks, this is a good way to start! :-)
72
Programming with NASM / Re: Printing of floating point values
« Last post by fredericopissarra on October 26, 2023, 06:21:19 PM »
You can separate the integer part from the fractional part with cvttss2si or cvttsd2si, and subtract the original values to get the fractional part (remember to obtain the absolute values). Then, for integer part, you can obtain the # of decimal algarisms using log10, and divide the integer value by 10, in sequence, util you get a zero quotient - getting the actual algarisms... For the fractional part, you just need to multiply them by 10 (until get 0.0 or the # of algarisms you want).

There are faster ways...
73
Programming with NASM / Re: Printing of floating point values
« Last post by andyz74 on October 26, 2023, 05:22:32 PM »
Hello Frank, and thanks for the welcome-wishes! :-)

As I think all over it, i just need the printf for "seeing" my values. In fact I do not really need this written out. 
But the printf-function seems to be cabaple of interpreting and displaying the packed BCD-Values.

For example I wrote a piece of code, which calculates different squareroots, which are at least partly even, without floating point. These are moved to registers, where I want to see them with a debugger.  Nevertheless, I can't see my values in the register; I assume, they are BCD-packed, which I assume, printf could write them OR I find another solution. (For unpacking the packed BCD ?)

Code: [Select]
; calculates square root

; nasm -f elf64 fpu_sq_root.asm
; ld -s -o fpu_sq_root fpu_sq_root.o


[bits 64]

global _start
 
section .data
    _msg  db '                ',0ah
    len_msg equ $-_msg
    val1: dq 16  ;declare quad word (double precision)
    val2: dq 9  ;declare quad word (double precision)
    val3: dq 4  ;declare quad word (double precision)
    val4: dq 1  ;declare quad word (double precision)
    val5: dq 0  ;declare quad word (double precision)
    val6: dq 0.25  ;declare quad word (double precision)
    val7: dq 2.25  ;declare quad word (double precision)
    val8: dq 6.25  ;declare quad word (double precision)
    val9: dq 12.25  ;declare quad word (double precision)
 
section .bss
    res: resq 1     ;reserve 1 quad word for result
 
section .text
    _start:
    nop
    nop
    nop
    nop
    nop
   
    fld qword [val1] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop

    fld qword [val2] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop

    fld qword [val3] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop

    fld qword [val4] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop

    fld qword [val5] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop

    fld qword [val6] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop
   
    fld qword [val7] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop
   
    fld qword [val8] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop
   
    fld qword [val9] ;load value into st0
    fsqrt           ;compute square root of st0 and store in st0
    fst qword [res] ;store st0 in result
    mov rbx, [res]
    nop
 
nop
nop
nop

mov rax,1
int 80h
    ;end program


=> Squareroots of the val's are computed and one after each stored in RBX.
If I debug the program with radare2, I see senseless numbers in RBX.
Are they packed BCD? If yes, how to unpack ?

Greetz and thanks, Andy

PS : the many "nop" are for me to be able to get orientation while debugging...
74
Programming with NASM / Re: Printing of floating point values
« Last post by Frank Kotler on October 26, 2023, 02:34:58 AM »
Hi Andy,
Welcome to the forum!

I think there should be a way to get gcc to do what you want. Not really a "Nasm question" but you need to know the answer ! , You can print floats without the C library, too but it may not be the best use of your time.

I hope someone can cone up with the right command line to gcc. 

Best,
Frank


75
Example Code / A little program to seek and write primes, with hardcoded maximum
« Last post by andyz74 on October 24, 2023, 09:25:27 PM »
Hi there !

Nothing special here, but I thought, maybe somebody is interested in such stuff.
A little program, which seeks and prints out primes until a maximum number (hardcoded in source).

Please don't blame me for parts of documentation is in german; I am not that good in english to translate all correct. ;-)

Done in Debian Linux, 64 bit.

Code: [Select]

; 64-bit  ... gibt Primzahlen aus
; by myself
; nasm -f elf64 primzahlen03.asm
; ld -s -o primzahlen03 primzahlen03.o
; time : 0,15 sec   (Optimierung : prüft nur von 1 bis Wurzel(zu überprüfende Zahl)
; Notiz : Wurzel nicht sehr schnell berechnet.


[bits 64]

; here just variables ---------------------------------------

SECTION .data

para_b db 0
para_w dq 0
primzahl_boolean db 0
ziffer db 0
zahl dd 0
crlf db 13,10


global _start ; global entry point export for ld

SECTION .text

_start:
jmp start2

get_wu: ; seeks iterative for squareroot of testing number...
; could be made way better, if I knew how...

push rax
push rdi
mov rax, [para_w]
mov rdi, rax ; rdi holds number
mov ebx, 0
nochzuklein:
inc ebx
mov eax, ebx
mul eax
cmp rdi, rax ; rechnet : zahl - (eax * eax) // solange größer 0, Wurzel noch nicht erreicht.
jns  nochzuklein
pop rdi
pop rax
ret



check_prim:
push rax
push rdx
push rsi
push rdi
mov byte [primzahl_boolean], 0 ; just a boolean-Variable...
mov rsi, [para_w] ; the number to check
call get_wu
mov rdi, rbx
nochmal:
mov rax,rsi
dec rdi
cmp rdi,1     
je ende
xor rdx, rdx
div rdi
cmp rdx,0
jnz nochmal
mov byte [primzahl_boolean], 1
ende:
pop rdi
pop rsi
pop rdx
pop rax
ret




zeilensprung:
push rax
push rbx
push rcx
push rdx
push rsi
push rdi
mov rax, 4
mov rbx, 1
mov rcx, crlf
mov rdx, 2
int 128
pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
pop rax
ret



print_dez:
push rax
push rbx
push rcx
push rdx
push rsi
push rdi
mov rax, [para_w]
xor rcx,rcx
mov rbx,10

schl1:
xor rdx,rdx
div rbx
push rdx
inc rcx
cmp rax,0

jnz schl1

schl2:
pop rdx
add dl,30h
mov [ziffer],dl
push rcx
mov rax, 4
mov rbx, 1
mov rcx, ziffer
mov rdx, 1
int 128
pop rcx
loop schl2

pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
pop rax
ret






start2:   ; here starts the proggi

mov rcx,200001   ; Number to start. hardcoded.

decrease_further:
mov [para_w], rcx
call check_prim

cmp byte [primzahl_boolean], 1     ; Prime ?
je no_prime

; mov [para_w], rcx
call print_dez

call zeilensprung

no_prime:       ; no prime !
sub rcx, 2
cmp rcx,1
jnz decrease_further


mov rax,1
int 128



76
Programming with NASM / Printing of floating point values
« Last post by andyz74 on October 24, 2023, 03:08:48 PM »
Hello people,

my question is, is there any (convenient) possibility to print floating point values, DESPITE printf() ?
I have problem with printf, because using gcc as linker, what I have to do in this case, produces alway error saying the "no-PIE"-stuff.  Do I really have to recompile GCC with the -np-PIE flag to work for me, or is there another possibility to print-out floating point-values?

My system is Debian 12, 64 bit.

Greetings, Andy
77
Example Code / Re: My own 64-bit `puts' instruction (No length required)
« Last post by fredericopissarra on October 05, 2023, 03:42:59 PM »
Maybe I'm not understanding how scas works, but isn't the result of scasb stored in rbx in 64-bit assembly?
Nope! scasb tests AL against ES:[RDI], setting the flags. repnz scasb does the same, RCX times while ZF=0.

Quote from: MediocreVeg1
Wouldn't the assembler get confused if I used 64-bit syscalls on 32-bit registers? Or if I put some arguments of a syscall in R?? registers and others in E?? registers?
Nope! E?? registers are the lower 32 bits of R?? registers. When you use a R?? register the instrunction is prefixed with a REX prefix (and, an immediate can be bigger), like, for example:

Code: [Select]
  mov eax,-1    ; B8 FF FF FF FF
  mov rax,-1    ; 48 B8 FF FF FF FF FF FF FF FF

When using EAX the upper 32 bits are automagically (hehe) zeroed.

Quote from: MediocreVeg1
Yeah, I put it into my procedure as well after you showed your example. Wouldn't this event be highly unlikely though? I think 2^32-1 is like 4294967295 bytes so every single byte after the starting address of the string would have to be non-zero, right?
That's why it doesn't make sense using R?? registers to hold string lengths...

[]s
Fred
78
Example Code / Re: My own 64-bit `puts' instruction (No length required)
« Last post by alCoPaUL on October 03, 2023, 05:08:47 PM »
you can just iterate displaying letters until it's \0..

so you can display a 1 gigabyte or more worth of strings and then cut it when it's \0.
79
Programming with NASM / Re: blsi
« Last post by Frank Kotler on September 26, 2023, 10:57:25 PM »
Right!
I wonder how suncowiam is making out with it?

Best,
Frank

80
Programming with NASM / Re: blsi
« Last post by fredericopissarra on September 26, 2023, 07:02:28 PM »
I believe BLSI instruction was introduced in Haswell microarchitecture. It is from BMI1 instruction set (VEX prefix) - so it is listed in Intel SDM. Of course old NASM versions don't have support for it.
Pages: 1 ... 6 7 [8] 9 10