Recent Posts

Pages: [1] 2 3 ... 10
1
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on Today at 01:26:24 PM »
Two figures attached, both from the ACM document "Symmetric List Processor," author Joseph Weizenbaum.

The part I'm working on now allows a user to enter a textual representation of a list, like the one in Fig. 3, from the keyboard, and construct from it an internal list, as shown in the diagrams. Currently lists can only be constructed programmatically.  Will post an example of that next.
2
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on Today at 12:56:28 AM »
A lot of this is already implemented. Will send an example to give you a better idea of what SLIP all about.
3
Programming with NASM / Re: Rotate 64-bit word
« Last post by fredericopissarra on June 23, 2021, 01:18:32 PM »
You still don't provide any reference of what calling convention is used by gfortran and what exactly you want to do, so the only thing I can do is to GUESS...

gfortran (and fortran, in general) is a shitty compiler in terms of optimization. You **could** make your function in fortran and take e look at the generated assembly code using -S option (and `-masm=intel` to see assembly in intel syntax, not AT&T syntax), but gfortran creates a bad code (in terms of optimization)...

OR.. you can use `gcc` (which is a very good compiler, in terms of optimization)... Here's a test of a function rotating 8 first chars in a string:
Code: [Select]
/* test.c */
#include <stdio.h>
#include <stdint.h>
#include <limits.h>

// Generic rotate left 1 QWORD 'bits' bits.
static uint64_t rotateleft ( uint64_t n, unsigned int bits )
{
  // Rotate bits to the left.
  n = ( n << bits ) |
      ( n >> sizeof ( uint64_t ) * CHAR_BIT - bits );

  return n;
}

// This will deal only with the 8 first chars.
// Rotating them 1 char to the right.
void krotateright ( char *s )
{
  // Convert the first 8 bytes to QWORD.
  uint64_t n = * ( uint64_t * ) s;

  // Rotate CHAT_BIT bits and stores back at the memory pointed by s.
  * ( uint64_t * ) s = rotateleft ( n, CHAR_BIT );
}

/* test */
int main ( void )
{
  char s[] = "ABCDEFG ";

  for ( int i = 0; i < 8; i++ )
  {
    printf ( "\"%s\" --rotate right 1 char--> ", s );
    krotateright ( s );
    printf ( "\"%s\"\n", s );
  }
}
Compiling and running:
Code: [Select]
$ cc -O2 -o test test.c
$ ./test
"ABCDEFG " --rotate right 1 char--> " ABCDEFG"
" ABCDEFG" --rotate right 1 char--> "G ABCDEF"
"G ABCDEF" --rotate right 1 char--> "FG ABCDE"
"FG ABCDE" --rotate right 1 char--> "EFG ABCD"
"EFG ABCD" --rotate right 1 char--> "DEFG ABC"
"DEFG ABC" --rotate right 1 char--> "CDEFG AB"
"CDEFG AB" --rotate right 1 char--> "BCDEFG A"
"BCDEFG A" --rotate right 1 char--> "ABCDEFG "
If you compile with:
Code: [Select]
$ cc -O2 -S -masm=intel -fcf-protection=none test.c -o test.sThis will generate the assembly listing of the entire code. Take a look at `test.s` and you'll see something like this:
Code: [Select]
krotateright:
  push  ebx
  mov   ecx, [esp+8]
  mov   eax, [ecx]
  mov   edx, [ecx+4]
  mov   ebx, eax
  shrd  eax, edx, 8
  shrd  edx, ebx, 8
  mov   [ecx], eax
  mov   [ecx+4], edx
  pop   ebx
  ret
Which isn't the best code possible, but it is a start.
4
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 22, 2021, 03:20:19 PM »
Finding that the AL register points at the "highest" character :

mov    al, 63

ABCDEFGH becomes ?BCDEFGH

Here's a much shorter version, with no blank counting.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

bits 32      ; to be sure.

struc lanorm_stk
      resd 1
.ptr: resd 1
endstruc

  global lanorm_
lanorm_:
  mov   eax,[esp+lanorm_stk.ptr]  ; get pointer

  mov   edx, [eax+4]   ; get QWORD from pointer.
  mov   eax, [eax]

  mov   ecx,edx

loop:
  cmp   al, 32
  jnz   finished
   shrd  edx,eax,8
   shrd  eax,ecx,8
  mov   ecx, edx
  jmp   loop
finished:
  ret
5
Programming with NASM / Re: Shadow Space
« Last post by ig on June 22, 2021, 11:49:49 AM »
The Shadow space is part of the calling convention - i.e. the standard describing how common functions communicate with each other.

If the whole program (well, not necessarily the whole program, let's say the piece of code where you're calling a function, i.e. both the caller and the called function) is under your control, written by you, then you can use whatever custom calling convention you want (pass arguments in arbitrary registers, get the result in something else than rax... whatever you want) and you don't have to create the Shadow space.

If, however, your code calls an "external" function (such as a Windows API function, or some code that was compiled by a C++ compiler... something out of the assembly), then you must create the Shadow space because the called function may be using it - and if you don't create it, it may overwrite unrelated parts of the stack and the program may crash after the execution returns from that function.
6
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 22, 2021, 01:31:13 AM »

mrice@debian10-uni:~/work$ ./test
       A
       A
A       

mrice@debian10-uni:~/work$ ./test
 ABCDEFG
 ABCDEFG
ABCDEFG

mrice@debian10-uni:~/work$ ./test
ABCDEFGH
ABCDEFGH
ABCDEFGH


The function, named lanorm.asm, because that's what it's called by the other code.

 bits 32      ; to be sure.

struc lanorm_stk
      resd 1
.ptr: resd 1
endstruc

  global lanorm_
lanorm_:
  mov   eax,[esp+lanorm_stk.ptr]  ; get pointer

  mov   edx, [eax+4]   ; get QWORD from pointer.
  mov   eax, [eax]

  push   eax      ;save eax
  mov esi, 0      ;initialize counter for blanks

l1:
  cmp al, 32      ;search EAX for blanks
  jnz l2
  inc esi
l2:
  ror eax, 8
  cmp al, 32
  jnz l3
  inc esi
l3:
  ror eax, 8
  cmp al, 32
  jnz l4
  inc esi
l4:
  ror eax, 8
  cmp al, 32
  jnz l5
  inc esi
l5:
  mov eax, edx      ;search EDX for blanks
l6:
  cmp al, 32
  jnz l7
  inc esi
l7:
  ror eax, 8
  cmp al, 32
  jnz l8
  inc esi
l8:
  ror eax, 8
  cmp al, 32
  jnz l9
  inc esi
l9:
  ror eax, 8
  cmp al, 32
  jnz finished
  inc esi
finished:
  pop   eax      ;restore EAX
  mov   ecx, edx
l10:
  shrd  edx,eax,8
  shrd  eax,ecx,8
  mov   ecx, edx
  dec esi
  jnz l10
  ret   
7
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 21, 2021, 07:21:18 PM »
Got my code working.  EAX gets used and abused in the blank search, so I pushed it before, and popped it after. Will post code later. May be a way to make it briefer now that we have it working.

mrice@debian10-uni:~/work$ ./test
    ABCD
    ABCD
ABCD   
mrice@debian10-uni:~/work$ ./test
  ABCDEF
  ABCDEF
ABCDEF 
mrice@debian10-uni:~/work$
8
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 21, 2021, 06:27:04 PM »
Just ran the code you supplied:

ABCD   
ABCD   
       0
mrice@debian10-uni:~/work$ ./test
ABCDEF 
ABCDEF 
       0
mrice@debiIan10-uni:~/work$ ./test
A       
A       
       0

Let me know if you have any changes.

9
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 21, 2021, 02:02:27 PM »
Code I sent worked fine after changing ECX to ESI.

Added this line up before the rotations to save EAX

;  mov   ebx, eax   ;save EAX

It alone caused a seg-fault. I commented it out and, suprise! - it worked again.

Also, after label "finished:", I swapped these

  mov   eax, esi
  mov   edx, 0

for

;  mov   eax, ebx   ;restore EAX
;
;  mov   ecx, edx
;
;l10:
;  shrd  edx,eax,8
;  shrd  eax,ecx,8
;  mov   ecx, edx
;  dec esi
;  jnz l10

Can you think of any reason why using EBX would cause a seg-fault?

 
10
Programming with NASM / Re: Rotate 64-bit word
« Last post by mjrice on June 21, 2021, 01:55:55 AM »
Welcome back. I think you're correct about the strings.

I've been busy. After my last message I poked around some and finally wrote some code that will count blanks in my 8 character 64-bit variables. I'm using ECX as a counter (code below). Tried using ESI so I wouldn't have to set it for the later shifting but got segmentation errors. Compiler, don't know which, perhaps both, seem to flake out on me, was giving me segmentation errors for the code below earlier, so I removed both NASM and gfortran and reinstalled both, after rebooting. The code then ran fine. Weird, huh?  I'll try ESI again tomorrow. I knew the code was good because when I finally got it to run I emailed a copy to myself. I'm aware that EAX must be restored before
shifting occurs. Will try out your code tomorrow.

-mjrice


mrice@debian10-uni:~/work$ ./test
ABCDEFG
ABCDEFG
       1
mrice@debian10-uni:~/work$ ./test
A       
A       
       7

;;;;;;;;;;;;;;;;;;;

  bits 32      ; to be sure.

struc kjunk_stk
      resd 1
.ptr: resd 1
endstruc

  global kjunk_
kjunk_:
  mov   eax,[esp+kjunk_stk.ptr]  ; get pointer

  mov   edx, [eax+4]   ; get QWORD from pointer.
  mov   eax, [eax]

  mov ecx, 0

l1:
  cmp al, 32
  jnz l2
  inc ecx
l2:
  ror eax, 8
  cmp al, 32
  jnz l3
  inc ecx
l3:
  ror eax, 8
  cmp al, 32
  jnz l4
  inc ecx
l4:
  ror eax, 8
  cmp al, 32
  jnz l5
  inc ecx
l5:
;  ror eax, 8
  mov eax, edx

l6:
  cmp al, 32
  jnz l7
  inc ecx
l7:
  ror eax, 8
  cmp al, 32
  jnz l8
  inc ecx
l8:
  ror eax, 8
  cmp al, 32
  jnz l9
  inc ecx
l9:
  ror eax, 8
  cmp al, 32
  jnz finished
  inc ecx

finished:
  mov eax, ecx
  mov edx, 0
ret



Pages: [1] 2 3 ... 10