Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If God had a beard, he'd be a UNIX programmer.


devel / comp.os.cpm / prevent a possible overflow when adding in a 16-bit processor register

SubjectAuthor
* prevent a possible overflow when adding in a 16-bit processor registerАндрей Никитин
+- Re: prevent a possible overflow when adding in a 16-bit processor registerfridtjof.ma...@gmail.com
`* Re: prevent a possible overflow when adding in a 16-bit processor registerfridtjof.ma...@gmail.com
 `* Re: prevent a possible overflow when adding in a 16-bit processor registerАндрей Никитин
  +- Re: prevent a possible overflow when adding in a 16-bit processor registerfridtjof.ma...@gmail.com
  `* Re: prevent a possible overflow when adding in a 16-bit processor registerfridtjof.ma...@gmail.com
   `- Re: prevent a possible overflow when adding in a 16-bit processor registerАндрей Никитин

1
prevent a possible overflow when adding in a 16-bit processor register

<6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2830&group=comp.os.cpm#2830

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:a37:62cb:: with SMTP id w194mr19477310qkb.189.1634067622139;
Tue, 12 Oct 2021 12:40:22 -0700 (PDT)
X-Received: by 2002:a9d:714b:: with SMTP id y11mr5816494otj.289.1634067621910;
Tue, 12 Oct 2021 12:40:21 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Tue, 12 Oct 2021 12:40:20 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=77.37.209.254; posting-account=g8ksUwoAAAC0mgCsQ1LJvKj1k2HoKsbw
NNTP-Posting-Host: 77.37.209.254
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
Subject: prevent a possible overflow when adding in a 16-bit processor register
From: nikitinp...@gmail.com (Андрей Никитин)
Injection-Date: Tue, 12 Oct 2021 19:40:22 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 21
 by: Андрей Ник - Tue, 12 Oct 2021 19:40 UTC

When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:

ld l, (ix+6)
ld h, (ix+7)
inc hl
ld a, l
and 0feh
ld l, a
ld a, h
ld (ix+6), l
ld (ix+7), h
ld de, (pointer2)
add hl, de
ex de, hl
ld hl, (pointer1)
call wrelop
jr nc, label1

Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
Maybe someone has come across a similar one and knows how these actions can be programmed in C.

Andrey

Re: prevent a possible overflow when adding in a 16-bit processor register

<7accbf8c-7e56-4aa3-9a99-447458ba626fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2831&group=comp.os.cpm#2831

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:a05:622a:652:: with SMTP id a18mr132576qtb.0.1634077466660;
Tue, 12 Oct 2021 15:24:26 -0700 (PDT)
X-Received: by 2002:a05:6830:2498:: with SMTP id u24mr16267474ots.151.1634077466390;
Tue, 12 Oct 2021 15:24:26 -0700 (PDT)
Path: rocksolid2!i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Tue, 12 Oct 2021 15:24:26 -0700 (PDT)
In-Reply-To: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:f2c0:938c:1700:6385:f4d1:187b:7b6c;
posting-account=KOfC_woAAAC0YBGf-3r2aV5g2Aifd9jO
NNTP-Posting-Host: 2607:f2c0:938c:1700:6385:f4d1:187b:7b6c
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7accbf8c-7e56-4aa3-9a99-447458ba626fn@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: fridtjof...@gmail.com (fridtjof.ma...@gmail.com)
Injection-Date: Tue, 12 Oct 2021 22:24:26 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 32
 by: fridtjof.ma...@gmail - Tue, 12 Oct 2021 22:24 UTC

On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
>
> ld l, (ix+6)
> ld h, (ix+7)
> inc hl
> ld a, l
> and 0feh
> ld l, a
> ld a, h
> ld (ix+6), l
> ld (ix+7), h
> ld de, (pointer2)
> add hl, de
> ex de, hl
> ld hl, (pointer1)
> call wrelop
> jr nc, label1
>
> Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> Maybe someone has come across a similar one and knows how these actions can be programmed in C.
>
> Andrey
Andrey

Sure -- look at my am9511 emulator

https://github.com/ratboy666/am9511

and, in specific, file ova.c (overflow arithmetic). It shows how to do overflow checking on add, subtract, and multiply.

Have fun!
Fred Weigel

Re: prevent a possible overflow when adding in a 16-bit processor register

<cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2832&group=comp.os.cpm#2832

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:ac8:5ac7:: with SMTP id d7mr26518133qtd.382.1634086636068;
Tue, 12 Oct 2021 17:57:16 -0700 (PDT)
X-Received: by 2002:aca:6549:: with SMTP id j9mr6208070oiw.126.1634086635826;
Tue, 12 Oct 2021 17:57:15 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Tue, 12 Oct 2021 17:57:15 -0700 (PDT)
In-Reply-To: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:f2c0:938c:1700:6385:f4d1:187b:7b6c;
posting-account=KOfC_woAAAC0YBGf-3r2aV5g2Aifd9jO
NNTP-Posting-Host: 2607:f2c0:938c:1700:6385:f4d1:187b:7b6c
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: fridtjof...@gmail.com (fridtjof.ma...@gmail.com)
Injection-Date: Wed, 13 Oct 2021 00:57:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 39
 by: fridtjof.ma...@gmail - Wed, 13 Oct 2021 00:57 UTC

On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
>
> ld l, (ix+6)
> ld h, (ix+7)
> inc hl
> ld a, l
> and 0feh
> ld l, a
> ld a, h
> ld (ix+6), l
> ld (ix+7), h
> ld de, (pointer2)
> add hl, de
> ex de, hl
> ld hl, (pointer1)
> call wrelop
> jr nc, label1
>
> Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> Maybe someone has come across a similar one and knows how these actions can be programmed in C.
>
> Andrey
Andrey

Anyway, there is no overflow here at all -- what is the type of pointer2? Note that if p is a pointer, p+n
is numeric p + (n * sizeof(*p)): in other words, this calculation:

char *p2 = (char *)p;
p = p + (n * sizeof(*p));
p2 = (void *)p;

Now, it looks like ix+6 and ix+7 are the "n", appears to do n + 1, and then makes it an even number (and 0feh)
Can I see the C source for this production?

Note that ova.c that I pointed you at earlier does overflow detection for C arithmetic. And it works on both Hi-Tech C
on the Z80, and GCC on modern platforms. It is reasonably efficient on both of those. Note that carry is returned by the
add/subtract, and overflow is detected after the add/subtract.

FredW

Re: prevent a possible overflow when adding in a 16-bit processor register

<b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2833&group=comp.os.cpm#2833

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:a37:6801:: with SMTP id d1mr23608662qkc.526.1634123422999;
Wed, 13 Oct 2021 04:10:22 -0700 (PDT)
X-Received: by 2002:a9d:609:: with SMTP id 9mr31019958otn.231.1634123422709;
Wed, 13 Oct 2021 04:10:22 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Wed, 13 Oct 2021 04:10:22 -0700 (PDT)
In-Reply-To: <cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=77.37.209.254; posting-account=g8ksUwoAAAC0mgCsQ1LJvKj1k2HoKsbw
NNTP-Posting-Host: 77.37.209.254
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com> <cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: nikitinp...@gmail.com (Андрей Никитин)
Injection-Date: Wed, 13 Oct 2021 11:10:22 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 74
 by: Андрей Ник - Wed, 13 Oct 2021 11:10 UTC

среда, 13 октября 2021 г. в 03:57:16 UTC+3, fridtjof.ma...@gmail.com:
> On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> > When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
> >
> > ld l, (ix+6)
> > ld h, (ix+7)
> > inc hl
> > ld a, l
> > and 0feh
> > ld l, a
> > ld a, h
> > ld (ix+6), l
> > ld (ix+7), h
> > ld de, (pointer2)
> > add hl, de
> > ex de, hl
> > ld hl, (pointer1)
> > call wrelop
> > jr nc, label1
> >
> > Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> > Maybe someone has come across a similar one and knows how these actions can be programmed in C.
> >
> > Andrey
> Andrey
>
> Anyway, there is no overflow here at all -- what is the type of pointer2? Note that if p is a pointer, p+n
> is numeric p + (n * sizeof(*p)): in other words, this calculation:
>
> char *p2 = (char *)p;
> p = p + (n * sizeof(*p));
> p2 = (void *)p;
>
> Now, it looks like ix+6 and ix+7 are the "n", appears to do n + 1, and then makes it an even number (and 0feh)
> Can I see the C source for this production?
>
> Note that ova.c that I pointed you at earlier does overflow detection for C arithmetic. And it works on both Hi-Tech C
> on the Z80, and GCC on modern platforms. It is reasonably efficient on both of those. Note that carry is returned by the
> add/subtract, and overflow is detected after the add/subtract.
>
> FredW

This is the memory allocation function:
The recreated C code is as follows:
char * alloc_mem (int size) {
char * l1;
register char * st;

if (word_707a <(word_7082 + (size = (size + 1) & 0xFE))) {
if ((word_7082 = sbrk (512)) == (char *) - 1)
pr_error ("Out of memory in% s", name_fun);
word_707a = sbrk (0);
}
st = word_7082;
word_7082 + = size;
l1 = st;
while (size--! = 0) * (l1 ++) = 0; // Clearing the allocated memory area
return st;
} However, the first if statement in this function generates code that is different from the one in the executable image.
I cannot understand why these actions (size = (size + 1) & 0xFE) and how important they are.

Andrey

Re: prevent a possible overflow when adding in a 16-bit processor register

<41b49294-85ba-4c6f-8752-91f962d19593n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2834&group=comp.os.cpm#2834

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:ac8:7f44:: with SMTP id g4mr18714108qtk.130.1634136960122;
Wed, 13 Oct 2021 07:56:00 -0700 (PDT)
X-Received: by 2002:aca:3555:: with SMTP id c82mr8545841oia.29.1634136959361;
Wed, 13 Oct 2021 07:55:59 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Wed, 13 Oct 2021 07:55:59 -0700 (PDT)
In-Reply-To: <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:f2c0:938c:1700:6385:f4d1:187b:7b6c;
posting-account=KOfC_woAAAC0YBGf-3r2aV5g2Aifd9jO
NNTP-Posting-Host: 2607:f2c0:938c:1700:6385:f4d1:187b:7b6c
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
<cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com> <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <41b49294-85ba-4c6f-8752-91f962d19593n@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: fridtjof...@gmail.com (fridtjof.ma...@gmail.com)
Injection-Date: Wed, 13 Oct 2021 14:56:00 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 99
 by: fridtjof.ma...@gmail - Wed, 13 Oct 2021 14:55 UTC

On Wednesday, October 13, 2021 at 7:10:23 AM UTC-4, nikiti...@gmail.com wrote:
> среда, 13 октября 2021 г. в 03:57:16 UTC+3, fridtjof.ma...@gmail.com:
> > On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> > > When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
> > >
> > > ld l, (ix+6)
> > > ld h, (ix+7)
> > > inc hl
> > > ld a, l
> > > and 0feh
> > > ld l, a
> > > ld a, h
> > > ld (ix+6), l
> > > ld (ix+7), h
> > > ld de, (pointer2)
> > > add hl, de
> > > ex de, hl
> > > ld hl, (pointer1)
> > > call wrelop
> > > jr nc, label1
> > >
> > > Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> > > Maybe someone has come across a similar one and knows how these actions can be programmed in C.
> > >
> > > Andrey
> > Andrey
> >
> > Anyway, there is no overflow here at all -- what is the type of pointer2? Note that if p is a pointer, p+n
> > is numeric p + (n * sizeof(*p)): in other words, this calculation:
> >
> > char *p2 = (char *)p;
> > p = p + (n * sizeof(*p));
> > p2 = (void *)p;
> >
> > Now, it looks like ix+6 and ix+7 are the "n", appears to do n + 1, and then makes it an even number (and 0feh)
> > Can I see the C source for this production?
> >
> > Note that ova.c that I pointed you at earlier does overflow detection for C arithmetic. And it works on both Hi-Tech C
> > on the Z80, and GCC on modern platforms. It is reasonably efficient on both of those. Note that carry is returned by the
> > add/subtract, and overflow is detected after the add/subtract.
> >
> > FredW
> This is the memory allocation function:
> The recreated C code is as follows:
> char * alloc_mem (int size) {
> char * l1;
> register char * st;
>
> if (word_707a <(word_7082 + (size = (size + 1) & 0xFE))) {
> if ((word_7082 = sbrk (512)) == (char *) - 1)
> pr_error ("Out of memory in% s", name_fun);
> word_707a = sbrk (0);
> }
> st = word_7082;
> word_7082 + = size;
> l1 = st;
> while (size--! = 0) * (l1 ++) = 0; // Clearing the allocated memory area
> return st;
> }
> However, the first if statement in this function generates code that is different from the one in the executable image.
> I cannot understand why these actions (size = (size + 1) & 0xFE) and how important they are.
>
> Andrey
Andrey

Ah... the first line is actually

if (word_707a < (word_7082 + (size = (size + 1) & 0xFFFE)))

Let us look at

size = (size + 1) &0xfffe

what this is doing is making size even: say, size is 13, 13 + 1 is 14 and 14 & 0xfffe is still 14. Say size is 14, 14 + 1 is 15, 15 & 0xfffe
is 14. So, size ends up being either size or the next higher even number. We then make sure the allocation fits into the already
allocated space. If it does we end up at st = word_7082. If not, we use sbrk() to add more memory,

How important? The code just ensures integer (16 bit) addressing -- so the least significant bit should always be zero on
allocated addresses.. may be important elsewhere (even+even = even -- but note that if address is odd, odd+even is odd --
if size is always even, the least significant bit will never change).

FredW

Re: prevent a possible overflow when adding in a 16-bit processor register

<0ad35842-d5b8-4350-b650-005aa11865e0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2835&group=comp.os.cpm#2835

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:a05:622a:1754:: with SMTP id l20mr766102qtk.309.1634137195367;
Wed, 13 Oct 2021 07:59:55 -0700 (PDT)
X-Received: by 2002:a9d:12b2:: with SMTP id g47mr20303695otg.227.1634137195140;
Wed, 13 Oct 2021 07:59:55 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Wed, 13 Oct 2021 07:59:54 -0700 (PDT)
In-Reply-To: <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:f2c0:938c:1700:6385:f4d1:187b:7b6c;
posting-account=KOfC_woAAAC0YBGf-3r2aV5g2Aifd9jO
NNTP-Posting-Host: 2607:f2c0:938c:1700:6385:f4d1:187b:7b6c
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
<cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com> <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0ad35842-d5b8-4350-b650-005aa11865e0n@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: fridtjof...@gmail.com (fridtjof.ma...@gmail.com)
Injection-Date: Wed, 13 Oct 2021 14:59:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 97
 by: fridtjof.ma...@gmail - Wed, 13 Oct 2021 14:59 UTC

On Wednesday, October 13, 2021 at 7:10:23 AM UTC-4, nikiti...@gmail.com wrote:
> среда, 13 октября 2021 г. в 03:57:16 UTC+3, fridtjof.ma...@gmail.com:
> > On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> > > When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
> > >
> > > ld l, (ix+6)
> > > ld h, (ix+7)
> > > inc hl
> > > ld a, l
> > > and 0feh
> > > ld l, a
> > > ld a, h
> > > ld (ix+6), l
> > > ld (ix+7), h
> > > ld de, (pointer2)
> > > add hl, de
> > > ex de, hl
> > > ld hl, (pointer1)
> > > call wrelop
> > > jr nc, label1
> > >
> > > Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> > > Maybe someone has come across a similar one and knows how these actions can be programmed in C.
> > >
> > > Andrey
> > Andrey
> >
> > Anyway, there is no overflow here at all -- what is the type of pointer2? Note that if p is a pointer, p+n
> > is numeric p + (n * sizeof(*p)): in other words, this calculation:
> >
> > char *p2 = (char *)p;
> > p = p + (n * sizeof(*p));
> > p2 = (void *)p;
> >
> > Now, it looks like ix+6 and ix+7 are the "n", appears to do n + 1, and then makes it an even number (and 0feh)
> > Can I see the C source for this production?
> >
> > Note that ova.c that I pointed you at earlier does overflow detection for C arithmetic. And it works on both Hi-Tech C
> > on the Z80, and GCC on modern platforms. It is reasonably efficient on both of those. Note that carry is returned by the
> > add/subtract, and overflow is detected after the add/subtract.
> >
> > FredW
> This is the memory allocation function:
> The recreated C code is as follows:
> char * alloc_mem (int size) {
> char * l1;
> register char * st;
>
> if (word_707a <(word_7082 + (size = (size + 1) & 0xFE))) {
> if ((word_7082 = sbrk (512)) == (char *) - 1)
> pr_error ("Out of memory in% s", name_fun);
> word_707a = sbrk (0);
> }
> st = word_7082;
> word_7082 + = size;
> l1 = st;
> while (size--! = 0) * (l1 ++) = 0; // Clearing the allocated memory area
> return st;
> }
> However, the first if statement in this function generates code that is different from the one in the executable image.
> I cannot understand why these actions (size = (size + 1) & 0xFE) and how important they are.
>
> Andrey
Andrey

Was the peephole optimizer used? It looks like a production was removed:

mov a,h
and 0ffh
mov h,a

is "missing" -- note that *if* the code were actually &0xFE, it should have been:

mov a,l
and 0feh
mov l,a
mov h,0

(or something along those lines)

or, the compiler code generator is smart enough to deal with converting word into byte as needed.

FredW

Re: prevent a possible overflow when adding in a 16-bit processor register

<85272f6c-a1b2-472d-9a3c-8a1d47327eccn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2836&group=comp.os.cpm#2836

  copy link   Newsgroups: comp.os.cpm
X-Received: by 2002:a05:620a:4044:: with SMTP id i4mr450309qko.301.1634145609781;
Wed, 13 Oct 2021 10:20:09 -0700 (PDT)
X-Received: by 2002:aca:6549:: with SMTP id j9mr9546199oiw.126.1634145609527;
Wed, 13 Oct 2021 10:20:09 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.os.cpm
Date: Wed, 13 Oct 2021 10:20:09 -0700 (PDT)
In-Reply-To: <0ad35842-d5b8-4350-b650-005aa11865e0n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=77.37.209.254; posting-account=g8ksUwoAAAC0mgCsQ1LJvKj1k2HoKsbw
NNTP-Posting-Host: 77.37.209.254
References: <6e3d9450-b6f7-4951-8a73-5b9203772463n@googlegroups.com>
<cb9fc0e0-1cf7-450f-9c43-bb13e3f7cc9bn@googlegroups.com> <b484c1c0-d272-4720-91a3-e66a35dbb420n@googlegroups.com>
<0ad35842-d5b8-4350-b650-005aa11865e0n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <85272f6c-a1b2-472d-9a3c-8a1d47327eccn@googlegroups.com>
Subject: Re: prevent a possible overflow when adding in a 16-bit processor register
From: nikitinp...@gmail.com (Андрей Никитин)
Injection-Date: Wed, 13 Oct 2021 17:20:09 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 104
 by: Андрей Ник - Wed, 13 Oct 2021 17:20 UTC

среда, 13 октября 2021 г. в 17:59:55 UTC+3, fridtjof.ma...@gmail.com:
> On Wednesday, October 13, 2021 at 7:10:23 AM UTC-4, nikiti...@gmail.com wrote:
> > среда, 13 октября 2021 г. в 03:57:16 UTC+3, fridtjof.ma...@gmail.com:
> > > On Tuesday, October 12, 2021 at 3:40:22 PM UTC-4, nikiti...@gmail.com wrote:
> > > > When adding an integer n to pointer2 (16-bit unsigned number) and comparing it to pointer1, the following z80 assembly code is used:
> > > >
> > > > ld l, (ix+6)
> > > > ld h, (ix+7)
> > > > inc hl
> > > > ld a, l
> > > > and 0feh
> > > > ld l, a
> > > > ld a, h
> > > > ld (ix+6), l
> > > > ld (ix+7), h
> > > > ld de, (pointer2)
> > > > add hl, de
> > > > ex de, hl
> > > > ld hl, (pointer1)
> > > > call wrelop
> > > > jr nc, label1
> > > >
> > > > Most likely this is done to prevent a possible overflow when adding in a 16-bit processor register.
> > > > Maybe someone has come across a similar one and knows how these actions can be programmed in C.
> > > >
> > > > Andrey
> > > Andrey
> > >
> > > Anyway, there is no overflow here at all -- what is the type of pointer2? Note that if p is a pointer, p+n
> > > is numeric p + (n * sizeof(*p)): in other words, this calculation:
> > >
> > > char *p2 = (char *)p;
> > > p = p + (n * sizeof(*p));
> > > p2 = (void *)p;
> > >
> > > Now, it looks like ix+6 and ix+7 are the "n", appears to do n + 1, and then makes it an even number (and 0feh)
> > > Can I see the C source for this production?
> > >
> > > Note that ova.c that I pointed you at earlier does overflow detection for C arithmetic. And it works on both Hi-Tech C
> > > on the Z80, and GCC on modern platforms. It is reasonably efficient on both of those. Note that carry is returned by the
> > > add/subtract, and overflow is detected after the add/subtract.
> > >
> > > FredW
> > This is the memory allocation function:
> > The recreated C code is as follows:
> > char * alloc_mem (int size) {
> > char * l1;
> > register char * st;
> >
> > if (word_707a <(word_7082 + (size = (size + 1) & 0xFE))) {
> > if ((word_7082 = sbrk (512)) == (char *) - 1)
> > pr_error ("Out of memory in% s", name_fun);
> > word_707a = sbrk (0);
> > }
> > st = word_7082;
> > word_7082 + = size;
> > l1 = st;
> > while (size--! = 0) * (l1 ++) = 0; // Clearing the allocated memory area
> > return st;
> > }
> > However, the first if statement in this function generates code that is different from the one in the executable image.
> > I cannot understand why these actions (size = (size + 1) & 0xFE) and how important they are.
> >
> > Andrey
> Andrey
>
> Was the peephole optimizer used? It looks like a production was removed:
>
> mov a,h
> and 0ffh
> mov h,a
>
> is "missing" -- note that *if* the code were actually &0xFE, it should have been:
>
> mov a,l
> and 0feh
> mov l,a
> mov h,0
>
> (or something along those lines)
>
> or, the compiler code generator is smart enough to deal with converting word into byte as needed.
>
> FredW

Thanks. I understood.

Andrey

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor