Recent Posts

Pages: [1] 2 3 ... 10
1
Programming with NASM / Re: Try Catch Exceptions implementation
« Last post by stephane on February 08, 2025, 01:56:23 PM »
Hi everyone!

I've been away for a long time (from programming, and from online social activities), due to personal reasons; I'm back with my desire to learn Assembly, again.
I've just seen your answers, and I just wanted to thank you, even if it comes 3 years later, I appreciate very much!

As for my question in this topic, I don't remember why I wanted to explore it, but your answers help me realize I have more important things to learn first, than trying to implement these kind of features.

Thank you all, for your courtesy, kindness, and interesting answers!
2
Programming with NASM / Re: Problem with calculating root
« Last post by andyz74 on January 30, 2025, 09:37:52 PM »
Main topic for me is  learning Assembler a bit. :-)
Good night now, in Germany we habe 10 pm, time to sleep.
3
Programming with NASM / Re: Problem with calculating root
« Last post by fredericopissarra on January 30, 2025, 08:44:01 PM »
I have another version of my program, in which I use the fp87 for getting the squareroot.  I test both.

The huge amount of divisions to see, what is prime and what not, is in the moment done by normal DIV command. But later, I will try to do this with fp87, and a third version to do it with sse2.

You don't need floating point or square root... Here:
Code: [Select]
_Bool isprime( unsigned int n )
{
  unsigned int d;
  unsigned long long int f;

  if ( n <= 3 )
    return n >= 2;

  d = n % 6;
  if ( d != 1 && d != 5 )
    return 0;

  for ( f = 5; f * f <= n; f += 2 )
    if ( ( n % f ) == 0 )
      return 0;

  return 1;
}
4
Programming with NASM / Re: Problem with calculating root
« Last post by andyz74 on January 30, 2025, 08:04:23 PM »
I have another version of my program, in which I use the fp87 for getting the squareroot.  I test both.

The huge amount of divisions to see, what is prime and what not, is in the moment done by normal DIV command. But later, I will try to do this with fp87, and a third version to do it with sse2.
5
Programming with NASM / Re: Problem with calculating root
« Last post by fredericopissarra on January 30, 2025, 07:44:59 PM »
A note of caution... If you intend to calculate the square root of an unsigned long long int, the double precision has only 53 bits of precision (while unsigned long long int has 64)... So the first example is insufficient: You need a bigger floating point precision that is not available to SSE... You must use fp87 instead:
Code: [Select]
; Input: RDI = x
; Output RAX = sqrt(x)
squareroot:
  fild qword [rsp-8],rdi   ; using red zone here.
  fsqrt
  fistp qword [rsp-8]
  mov rax,[rsp-8]
  ret
6
Programming with NASM / Re: Problem with calculating root
« Last post by andyz74 on January 30, 2025, 07:31:00 PM »
This works great! Many thanks, Frederico!

For learning purposes I try to make programs to find primes. And to reduce calculation times I will divide the actual number through a cycle from [sqrt(actual number)] downto 3.   (I test only the odd ones...)
Therefor I need to calculate the squareroots.

Many thanks, I've learned just a little bit. :-)
7
Programming with NASM / Re: Problem with calculating root
« Last post by fredericopissarra on January 30, 2025, 07:07:16 PM »
I'll assume you just want to extract the square root of an integer value and get an integer result using `sqrtsd` instruction...

First: double type has 53 bits of precision and 11 bits in the expoent of the scale factor plus the signal bit. This means a double is 64 bits long (but with less precision than an long long int)... Let's say your function is defined to obey SysV ABI for x86-64 (not for Windows):

Code: [Select]
; unsigned long long int sqrtroot( unsigned long long int x );
; Entry: RDI = x
; Exit: RAX
  global sqrtroot

  align 4
sqrtroot:
  cvtsi2sd xmm0,rdi      ; Convert RDI to double scalar in XMM0.
  sqrtsd xmm0,xmm0       ; xmm0 = sqrt(xmm0)
  cvtsd2si rax,xmm0      ; Convert XMM0 double scalar to RAX (this convertion applies rounding).
  ret
For Windows change RDI to RCX.

If you want to use unsigned int, instead, you need to zero the upper 32 bits of RDI:
Code: [Select]
; unsigned int sqrtroot( unsigned int x );
; Entry: EDI = x
; Exit: EAX
  global sqrtroot

  align 4
sqrtroot:
  mov edi,edi
  cvtsi2sd xmm0,rdi      ; Convert RDI to double scalar in XMM0.
  sqrtsd xmm0,xmm0       ; xmm0 = sqrt(xmm0)
  cvtsd2si rax,xmm0      ; Convert XMM0 double scalar to RAX (this convertion applies rounding).
  ret
PS: GCC makes sure the upper double from xmm0 is zero beginning with pxor xmm0,xmm0, but we don't need to do this since we're dealing only with an scalar...
8
Programming with NASM / Re: Problem with calculating root
« Last post by fredericopissarra on January 30, 2025, 06:47:16 PM »
First of all: What do you want to do?
9
Programming with NASM / Problem with calculating root
« Last post by andyz74 on January 29, 2025, 09:35:26 PM »
Hello all of you,
I am sure, this is a absolutely stupid failure of mine, but I don' get it...  so, please have a look and see, if you are able to find the problem

Code: [Select]
get_wu:
mov rbx, qword [para_w] ; load my value to rbx to ensure I can see in debugger if it is right
movsd xmm1, qword [para_w]                                ;  get my value to xmm1 
sqrtsd xmm0, xmm1                                          ; take root of xmm1 and save it to xmm0
cvttsd2si rbx, xmm0                                               ; convert (and truncate) xmm0 to integer and store it in rbx
inc rbx
ret


in the debugger I see, that rbx and xmm1 are correct loaded with my value,
after the sqrtsd , I see "something" in xmm0, which I assume it is my packed calculated root
and after the cvttsd2si, I have always 0 (zero)  in my rbx. not a correct calculated root.

Please, what am I doing wrong?

Note:  I do not need the correct root, but the integer part has to be correct.  (sqrt(10)  => 3) is good for me
10
Programming with NASM / Re: Converting MASM TSR in DOS and NASM
« Last post by tysonprogrammer on January 04, 2025, 12:00:21 AM »
I found the error of my ways, I needed to use jmp far [cs:old_interupt9]

Now to see why it's not detecting the keys and disabling the normal CTRL+ALT+DEL

So I have this book that has an example TSR written in MASM, al it doe sit look at the keyboard flags and if the user presses CTRL+ALT+DEL is if change turns off the CTRL bit. If the user pressss CTRL_ALT_Rgt Shift+DEL is will original handler.

I pretty much have it  working except after it installs I can no longer type anything so I am guessing it's not calling the old handler. I am pretty sure I have the jump addresses incorrect but not sure how to get that working.

I had originally pasted int the MASM source but was concerned that it might be copyrighted so I removed it and I removed unrelated code from my version as I believe the issue is either setting the interrupt vector or the jumping to the original one. Perhaps I am not jumping correctly.

Code: [Select]
org 100h

main:
jmp setup

int9_handler:
sti                                 ; enable hardware interupts
pushf                               ; save regs and flags
push es
push ax
push di

pop di
pop ax
pop es
popf
jmp [cs:old_interupt9]

old_interupt9 dw 2 dup (?)
end_isr: db 1 dup (?)
; ----------------------------------------------
; Save copy of original interrupt 9 vector, and setup
; the address of the program as the new vector, terminate
; this program and leave int9_handler procedure in memory.
setup:
mov ax, 0x3509                      ; get int9 vector
int 0x21

mov word [old_interupt9], bx        ; save int9 vector
mov word [old_interupt9+2], es

mov ax, 0x2509                      ; set int9 vector
mov dx, int9_handler
int 0x21

mov ax, 0x3100                      ; terminate and stay resident
mov dx, end_isr                     ; point to end of resident code
shr dx, 4                           ; divide by 16
inc dx                              ; round forward one paragraph
int 0x21


What am I missing?


thanks,
Tyson
Pages: [1] 2 3 ... 10