Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.acorn.programmer > #2044
| From | Martin Bazley <martin.bazley@blueyonder.co.uk> |
|---|---|
| Newsgroups | comp.sys.acorn.programmer |
| Subject | Re: Unsigned integer comparison in Basic V |
| Message-ID | <2e75c2bc52.martin@blueyonder.co.uk> (permalink) |
| References | <9d64a2bc52.news@user.minijem.plus.com> |
| Organization | virginmedia.com |
| Date | 2012-08-09 23:56 +0100 |
The following bytes were arranged on 9 Aug 2012 by Richard Porter : > In the latest update to SiteMatch I needed to compare timestamps which > are 40-bit integers held in two words. Unfortunately comparing the > bottom word went wrong because it was treated as being signed. As a > result I had to write a little function to do an unsigned comparison > which involved testing whether each integer was less than zero or not. > > I was wondering if anyone has a neater solution to this problem, which > has no doubt been encountered many times. BASIC doesn't have unsigned integers, and this is extremely annoying. (What, for example, will happen when the file size limit is increased from 2GB to 4GB in the near future?) The way I would probably have done it is with an assembler routine such as the following: codelimit%=64 DIM code% codelimit%-1 FOR pass%=8 TO 10 STEP 2 P%=code%:L%=code%+codelimit% [OPT pass% .unsigned_le% CMP R0,R1 MOV R0,#0 MVNLS R0,#0 MOV PC,R14 .unsigned_lt% CMP R0,R1 MOV R0,#0 MVNLO R0,#0 MOV PC,R14 .unsigned_ge% CMP R0,R1 MOV R0,#0 MVNHS R0,#0 MOV PC,R14 .unsigned_gt% CMP R0,R1 MOV R0,#0 MVNHI R0,#0 MOV PC,R14 ] NEXT ...And then, to make it a bit nicer, you could have veneers like these: DEF FNunsigned_le(A%,B%)=USR(unsigned_le%) DEF FNunsigned_lt(A%,B%)=USR(unsigned_lt%) DEF FNunsigned_ge(A%,B%)=USR(unsigned_ge%) DEF FNunsigned_gt(A%,B%)=USR(unsigned_gt%) So, instead of writing "IF filesize1%<filesize2%", you'd write "IF FNunsigned_lt(filesize1%,filesize2%)". A bit unwieldy, I know, but that's BASIC for you. Generally speaking, mathematical operations with a large number of bits are easier to do in assembler, which has features like unsigned comparisons and the carry flag to help you. By the way, if you want to compare two numbers of more than 32 bits each (such as, say, 40-bit datestamps), you could try the following (completely untested!) extension: .unsigned_lt64% CMP R1,R3 CMPEQ R0,R2 MOV R0,#0 MVNLO R0,#0 MOV PC,R14 DEF FNunsigned_lt64(A%,B%,C%,D%)=USR(unsigned_lt64%) IF FNunsigned_lt64(lowbits1%,highbits1%,lowbits2%,highbits2%)... -- __<^>__ / _ _ \ It is written that Geeks shall inherit the Earth. ( ( |_| ) ) \_> <_/ ======================= Martin Bazley ==========================
Back to comp.sys.acorn.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unsigned integer comparison in Basic V Richard Porter <dontusethis@address.uk.invalid> - 2012-08-09 18:06 +0100
Re: Unsigned integer comparison in Basic V Martin Bazley <martin.bazley@blueyonder.co.uk> - 2012-08-09 23:56 +0100
Re: Unsigned integer comparison in Basic V Richard Porter <dontusethis@address.uk.invalid> - 2012-08-10 12:12 +0100
Re: Unsigned integer comparison in Basic V "Ste (news)" <steve@revi11.plus.com> - 2012-08-10 14:10 +0100
Re: Unsigned integer comparison in Basic V Jeremy Nicoll - news posts <jn.nntp.scrap007@wingsandbeaks.org.uk> - 2012-08-10 14:58 +0100
Re: Unsigned integer comparison in Basic V "Ste (news)" <steve@revi11.plus.com> - 2012-08-13 16:14 +0100
Re: Unsigned integer comparison in Basic V Steve Drain <steve@kappa.me.uk> - 2012-08-10 16:11 +0100
Re: Unsigned integer comparison in?Basic V Justin Fletcher <gerph@gerph.org> - 2012-08-10 15:25 +0100
Re: Unsigned integer comparison in Basic V jgh@arcade.demon.co.uk - 2012-08-10 15:49 -0700
Re: Unsigned integer comparison in Basic V Martin Bazley <martin.bazley@blueyonder.co.uk> - 2012-08-11 19:44 +0100
Re: Unsigned integer comparison in Basic V Richard Porter <dontusethis@address.uk.invalid> - 2012-08-13 16:19 +0100
csiph-web