Author Topic: Fun with CALL ABSOLUTE  (Read 5870 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Fun with CALL ABSOLUTE
« Reply #15 on: December 04, 2020, 10:54:37 pm »
Quote from Steve

Quote
DIM string1 AS STRING * 16384
DIM string2 AS STRING * 16384

DIM m1 AS _MEM, m2 AS _MEM
DIM b1 AS _BYTE, b2 AS _BYTE
m1 = _MEM(string1): m2 = _MEM(string2)

FOR i = 0 TO 16383
    _MEMGET m1, 1.OFFSET + i, b1
    _MEMGET m2, m2.OFFSET + i, b2
    b3 = b1 XOR b2
    ‘Do whatever you want with b3
NEXT

(Though in this case, I’d read as _UNSIGNED _INTEGER64’s, to reduce processes by 1/8th.)

@Steve

I am unclear of your comment in the (...) - do you mean having

 i as _UNSIGNED_INTEGER64

or for the whole line(s)

_MEMGET m1, 1.OFFSET + i, b1



Also, when you say reduce processes by 1/8 - many people use conflicting interpretations - so do you mean the final timings are

original - 1/8  = ~88% of original time

OR

original *1/8  = ~12% of original time


In any case, how would you write the most time-efficient code for the example task as above?
« Last Edit: December 04, 2020, 10:58:11 pm by Richard »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun with CALL ABSOLUTE
« Reply #16 on: December 05, 2020, 01:06:22 am »
DIM string1 AS STRING * 16384
DIM string2 AS STRING * 16384

DIM m1 AS _MEM, m2 AS _MEM
DIM b1 AS _UNSIGNED _INTEGER64, b2 AS _UNSIGNED _INTEGER64, b3 AS _UNSIGNED _INTEGER64
m1 = _MEM(string1): m2 = _MEM(string2)

FOR i = 0 TO 16383 STEP 8
    _MEMGET m1, m1.OFFSET + i, b1
    _MEMGET m2, m2.OFFSET + i, b2
    b3 = b1 XOR b2
    ‘Do whatever you want with b3
NEXT

b1, b2, b3 are all UINT64s above, so I’m reading and processing 8 bytes at a time, rather than 1, allowing me to STEP 8 in my FOR loop.  You’ll have 1/8th the reads and comparisons, being a lot faster in the end.
« Last Edit: December 05, 2020, 01:07:50 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Fun with CALL ABSOLUTE
« Reply #17 on: December 05, 2020, 01:36:42 am »
Would this work?


FOR i = 0 TO 16383 STEP 8
 
   _MEMGET m1, 1.OFFSET + i, b1
   _MEMGET m2, m2.OFFSET + i, b2
   _MEMPUT m2, m2.OFFSET + i, b2 XOR b1
   
NEXT
« Last Edit: December 05, 2020, 01:41:20 am by NOVARSEG »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun with CALL ABSOLUTE
« Reply #18 on: December 05, 2020, 02:04:39 am »
Would this work?


FOR i = 0 TO 16383 STEP 8
 
   _MEMGET m1, 1.OFFSET + i, b1
   _MEMGET m2, m2.OFFSET + i, b2
   _MEMPUT m2, m2.OFFSET + i, b2 XOR b1
   
NEXT

No idea without testing.  b1 XOR b2 gives what variable type as a return answer, by default?  Are you trying to put a SINGLE value where your UINT64 was?  You can try it and see, but it’s not the way I’d personally do things.  You might try specifying AS _UNSIGNED _INTEGER64 on the end of that last line, but honestly, I’m not certain how  permissive QB64 would be with that style syntax.

All I can do is say test it and experiment.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Fun with CALL ABSOLUTE
« Reply #19 on: December 05, 2020, 02:28:01 am »
Forgot that b3 was DIMed

**Encrypt
FOR i = 0 TO 16383 STEP 8
 
   _MEMGET m1, m1.OFFSET + i, b1
   _MEMGET m2, m2.OFFSET + i, b2
    b3 = b1 XOR b2
  _MEMPUT m2, m2.OFFSET + i, b3
   
NEXT

**Decrypt
FOR i = 0 TO 16383 STEP 8
 
   _MEMGET m1, m1.OFFSET + i, b1
   _MEMGET m2, m2.OFFSET + i, b2
    b3 = b1 XOR b2
  _MEMPUT m2, m2.OFFSET + i, b3
   
NEXT

****
OK there might be a faster way. In assembler you can do
****in general
mov si, offset string1
mov di, offset string2
mov eax, [si + bx]
XOR [di + bx], eax
**etc
4 bytes at a time from string1 XORed with same 4 bytes in string2
there are op code for 8 bytes too:  https://www.felixcloutier.com/x86/xor
To make that look QB-ish

   _MEMGET m1, m1.OFFSET + i, b1
   _XORPUT m2, m2.OFFSET + i, b1  ' 8bytes at a time
« Last Edit: December 05, 2020, 03:28:07 am by NOVARSEG »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun with CALL ABSOLUTE
« Reply #20 on: December 05, 2020, 07:33:37 am »
As much as you like assembler, I’m surprised you don’t just code in it exclusively.  Run both and give us some speed comparison tests.  How much faster and efficient is your assembly routine?  Is it worth the bother?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Fun with CALL ABSOLUTE
« Reply #21 on: December 05, 2020, 02:05:06 pm »
What Steve, not having... "Fun with CALL ABSOLUTE" yet? This guy reminds me of Chandler, from the QBasic Forum. His mind was stuck in 0's and 1's Land. Me, I have a book on Assembly Language. It was fun for a few routines, back in the QB days, and helped explain how the QB mouse functions worked, but nowadays, it just props up a crooked bar stool in the garage. It is very efficient at dong that.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Fun with CALL ABSOLUTE
« Reply #22 on: December 05, 2020, 06:29:00 pm »

Quote
As much as you like assembler, I’m surprised you don’t just code in it exclusively.  Run both and give us some speed comparison tests.  How much faster and efficient is your assembly routine?  Is it worth the bother?

Just showing another way to do that particular string operation.  However the _MID stuff is already fast and you would need a good reason to tweak what QB64 already has.   I'm very impressed with QB64 - it is miles ahead of any BASIC out there.

Anyway XOR in string operations is  especially useful for file encryption.   I'm sure C++ has an instruction that would essentially compile to the ASM code:
****in general
mov si, offset string1
mov di, offset string2
mov eax, [si + bx]
XOR [di + bx], eax
**etc

So I don't think it would be too hard to create
 _XORPUT m2, m2.OFFSET + i, b1 
I'm no expert in C++


« Last Edit: December 05, 2020, 06:33:38 pm by NOVARSEG »