Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / RTL

MemUtils

A community discussion post from the original code:zIRON forum.

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

macros output everything as is, and so uses could end up inside the middle of a function, which would be an incorrect syntax.

for macros, using or push/pop should be used.

e.g.

using(edi) {
  edi = 1;
}
0CodErr member
Posts
201
Threads
37
Location
Unknown

I am a bit rewrite memfill. Currently there is "using" for save edi instead of "push \ pop". Also at now used operators "&=" and "|=".

/*
/    Fill block of memory with specified value
/    $buf -- Pointer to block of memory to fill
/    $len -- Number of bytes to set to value
/    $ch_ -- Value to be set
*/
inline function m_memFill($buf, $len, $ch_) {
//   Copyright (c) 0CodErr, KolibriOS team
  using edi {
    eax = 0;
    al  = $ch_;
    edx = $len;
    edi = $buf;
    push edi
    ecx = eax;
    ah  = al;
    ch  = cl;
    ecx << 16;
    eax |= ecx;
    ecx = edx;
    ecx >> 2;
    edx &= 3;
    rep stosd;
    ecx = edx;
    rep stosb;
    pop eax
  }
  $return eax;
}
Colin admin
Posts
928
Threads
49
Location
Bulgaria
  • updated for next release.
0CodErr member
Posts
201
Threads
37
Location
Unknown

Tried to implement 16|32|64 bits compatible MemMove function.

Memory regions of the source area and the destination can be overlapped.

I hope that this will work :)

/*
/    Move memory from src to dst with specified length
/    dst -- Pointer to destination block of memory to move
/    src -- Pointer to source block of memory to move
/    len -- Count of bytes to move
/    Copyright (c) 0CodErr, KolibriOS team
*/
#set frame off;
procedure MemMove(char* dst, src; sysword len) {
uses gp_si, gp_di, gp_bx, gp_bp;

  const SIZEOF_SYSWORD_MINUS_1 = sizeof sysword - 1;
  const SIZEOF_SYSWORD_MUL_8_MINUS_1 = sizeof sysword * 8 - 1;

  gp_cx = len;
  if (gp_cx <> 0) {
    gp_di = dst;
    gp_si = src;
    gp_di == gp_si;      // cmp gp_di, gp_si
    if (ZF <> TRUE) {    // gp_di <> gp_si
      gp_bp = sizeof sysword;
      if (CF == FALSE) { // gp_di > gp_si
        // Move Up
        gp_di += gp_cx;
        gp_si += gp_cx;
        gp_si -= sizeof sysword;
        gp_di -= sizeof sysword;
        gp_dx  = SIZEOF_SYSWORD_MINUS_1;
        -gp_bp;          // neg gp_bp
      } else {           // gp_di < gp_si
        // Move Down
        gp_dx = 0;
      }

      gp_ax  = gp_cx;
      gp_ax &= SIZEOF_SYSWORD_MINUS_1;
      gp_cx >> $log2i(sizeof sysword);

    // at first copy by syswords
      repeat if (gp_cx > 0) {
        gp_bx = [gp_si];
        [gp_di] = gp_bx;
        gp_di += gp_bp;
        gp_si += gp_bp;
        gp_cx--;
      } until (ZF == TRUE)

      gp_cx  = gp_ax;
      gp_si += gp_dx;
      gp_di += gp_dx;
      [sysint]gp_bp >> SIZEOF_SYSWORD_MUL_8_MINUS_1; // if (gp_bp < 0) then (gp_bp = -1)
      gp_bp |= 1;                                    // if (gp_bp > 0) then (gp_bp = +1)

    // than copy by bytes
      repeat if (gp_cx > 0) {
        bl = [gp_si];
        [gp_di] = bl;
        gp_di += gp_bp;
        gp_si += gp_bp;
        gp_cx--;
      } until (ZF == TRUE)
    }
  }
}
#set frame on;

Some issue:

This ok sar used:

[sysint]gp_bp >> SIZEOF_SYSWORD_MUL_8_MINUS_1

But this shr used:

gp_bp >> [sysint]SIZEOF_SYSWORD_MUL_8_MINUS_1

Could you implement to write

if (ZF)

instead of

if (ZF <> TRUE)

and

if (!CF)

instead of

if (CF == FALSE)

It may be not so convenient to use constants like SIZEOF_SYSWORD_MUL_8_MINUS_1 and SIZEOF_SYSWORD_MINUS_1 instead of just inline (sizeof sysword - 1) and (sizeof sysword * 8 - 1) into code but currently it not supported.

This for test:

program PE32_CUI 'Test MemMove';

#include 'console.zir';

const test_str = 'Programming on ziron is very interesting!\x0';
char  buf1[64] = test_str;
char  buf2[64] = test_str;

println('Initial string: ', test_str);

eax = @buf1 + 24; edx = eax + 5;
MemMove(eax, edx, 18);
println('Move  backward: ', buf1);

eax = @buf2 + 24; edx = eax + 5;
MemMove(edx, eax, 18);
println('Move   forward: ', buf2);

wait_key();
ExitProcess(0);

This result of test:

Colin admin
Posts
928
Threads
49
Location
Bulgaria

This ok sar used: Code:

[sysint]gp_bp >> SIZEOF_SYSWORD_MUL_8_MINUS_1

But this shr used: Code:

gp_bp >> [sysint]SIZEOF_SYSWORD_MUL_8_MINUS_1

This works correct, Ziron uses the left member for precedence, in this case gp_bp is used, registers are default "as" uint.

This removes any confusion as to what the result should be since it is placed at the start of the line.

you could also do something along the lines of:

gp_bp as sysint;
gp_bp >> .....

However this will keep gp_bp defined as a sysint until you reset/change it.