binary mirroring? in basic

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
the7778
Posts: 14
Joined: Sat Oct 05, 2019 5:14 pm

binary mirroring? in basic

Post by the7778 »

same as last question, really. only this time i want to mirror an 8 bit decimal number
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: binary mirroring? in basic

Post by XavSnap »

Was...
i know this is a bit of a maths question but how to i find the decimal opposite of an 8 bit binary number? like inverting all the bits? so say 255 becomes 0 and 0 becomes 255.
like 11111111
00000000
No, "Who to mirror a binary value?"
"31" = "13"
"62" = "26"
"99" = "99"
"123456789"="987654321"... in decimal.
:oops:

But, "10100000" = 160 and "00000101"=5.

Not in Basic, it's too heavy.
You had to convert decimals in Binary, generate mirroring and convert the Binary to decimal !
Probably 1 hours to do this.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: binary mirroring? in basic

Post by XavSnap »

Code: Select all

     0  REM [HEX:\
21,32,23,CD,D8,09,01,08,\
00,09,3A,21,40,F5,79,B8,\
C8,0B,23,F1,CB,7F,17,20,\
04,36,1C,18,F0,36,1D,18,\
EC ]

10 POKE 16417,153
20 GOSUB 9000
21 PRINT B$
22 GOSUB 9100
23 PRINT B
30 STOP
  9000 RAND USR 16514
  9010 LET B$="XXXXXXXX"
  9020 RETURN
  9100 LET B=128 * VAL B$(1)+64 * VAL B$(2)+32 * VAL B$(3)+16 * VAL B$(4)+8 * VAL B$(5)+4 * VAL B$(6)+2 * VAL B$(7)+1 * VAL B$(8)
 9300 RETURN
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: binary mirroring? in basic

Post by XavSnap »

Hi,
If you had to use:
db $21 ; LD HL, $xx
dw 9010 ; Store Line number.
CALL $09D8 ; offset to HL
This function bugg the Basic monitor.

You had to Reste the right Basic NextLine, or ReadOffset, at the current read basic line !

Code: Select all

;; 
;; Compile with "tasm -80 -b program.asm program.p" 
;; 
 
#define db .byte ; TASM cross-assembler definitions 
#define dw .word 
#define ds .block 
#define org .org 
#define end .end 
 
   org     $4009 
 
;= System variables ============================================ 
 
   db 0     ;VERSN 
   dw 0     ;E_PPC 
   dw dfile      ;D_FILE 
   dw dfile+1    ;DF_CC 
   dw var   ;VARS 
   dw 0     ;DEST 
   dw var+1      ;E_LINE 
   dw last-1     ;CH_ADD 
   dw 0     ;X_PTR 
   dw last  ;STKBOT 
   dw last  ;STKEND 
   db 0     ;BERG 
   dw membot     ;MEM 
   db 0     ;not used 
   db 2     ;DF_SZ 
   dw 1     ;S_TOP 
   db $FF,$FF,$FF     ;LAST_K 
   db 55    ;MARGIN 
   dw dfile      ;NXTLIN 
   dw 0     ;OLDPPC 
   db 0     ;FLAGX 
   dw 0     ;STRLEN 
   dw $0C8D      ;T_ADDR 
   dw 0     ;SEED 
   dw $FFFF      ;FRAMES 
   db 0,0   ;COORDS 
   db $BC   ;PR_CC 
   db 33,24      ;S_POSN 
   db 01000000B  ;CDFLAG 
prtbuff:
   ds 33    ;Print buffer 
membot: 
   ds 30    ;Calculator´s memory area 
   ds 2     ;not used 
 
;= First BASIC line, asm code ================================== 
 
line0: 
   db 0,0 
   dw dfile-$-2 
   db $ea   ; REM 
 
   db $21 ; LD HL, $xx
   dw 9010 ; Store Line number.
	CALL $09D8 ; offset to HL
	LD BC,8 ; LET B$="xxxxxxxxxx"
	ADD HL,BC
	LD A,($4021)
LOOP1:
	PUSH AF
	LD A,C
	CP B
	JR Z,EXIT ; 8 bits>Exit
	DEC BC
	INC HL
	POP AF
	BIT 7,A
	RLA
	JR NZ,GET1
	LD (HL),$1C ; "0"
	JR LOOP1
GET1:
	LD (HL),$1D ; "1"
	JR LOOP1
EXIT:

   db $21 ; LD HL, $xx
   dw 9010 ; Store Line number.
	CALL $09D8 ; offset to HL
   RET
 ;;
   db $76   ;N/L
 
;- Display file -------------------------------------------- 
 
dfile: 
   db $76 
   db $76,$76,$76,$76,$76,$76,$76,$76 
   db $76,$76,$76,$76,$76,$76,$76,$76 
   db $76,$76,$76,$76,$76,$76,$76,$76 
 
;- BASIC-Variables ---------------------------------------- 
 
var: 
   db $80 
 
;- End of program area ---------------------------- 
 
last: 
 
   end 

A	.equ $26
B	.equ $27
C	.equ $28
D	.equ $29
E	.equ $2A
F	.equ $2B
G	.equ $2C
H	.equ $2D
I	.equ $2E
J	.equ $2F
K	.equ $30
L	.equ $31
M	.equ $32
N	.equ $33
O	.equ $34
P	.equ $35
Q	.equ $36
R	.equ $37
S	.equ $38
T	.equ $39
U	.equ $3A
V	.equ $3B
W	.equ $3C
X	.equ $3D
Y	.equ $3E
Z	.equ $3F
_	.equ $00
_0	.equ $1C
_1	.equ $1D
_2	.equ $1E
_3	.equ $1F
_4	.equ $20
_5	.equ $21
_6	.equ $22
_7	.equ $23
_8	.equ $24
_9	.equ $25
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: binary mirroring? in basic

Post by XavSnap »

Code: Select all

     0  REM [HEX:\
21,32,23,CD,D8,09,01,08,\
00,09,3A,21,40,F5,79,B8,\
28,10,0B,23,F1,CB,7F,17,\
20,04,36,1C,18,EF,36,1D,\
18,EB,21,32,23,CD,D8,09,\
C9 ]

5 LET D$=""
10 POKE 16417,212
20 GOSUB 9000
25 PRINT B$
30 FOR A=8 TO 1 STEP -1
40 LET D$=D$+B$(A)
50 NEXT A
60 LET B$=D$
70 GOSUB 9100
80 PRINT B
90 POKE 16417,B
100 GOSUB 9000
110 PRINT B$
1000 STOP

8999 REM DEC TO BIN
  9000 RAND USR 16514
  9010 LET B$="XXXXXXXX"
  9020 RETURN
  9099 REM BIN TO DEC
  9100 LET B=128 * VAL B$(1)+64 * VAL B$(2)+32 * VAL B$(3)+16 * VAL B$(4)+8 * VAL B$(5)+4 * VAL B$(6)+2 * VAL B$(7)+1 * VAL B$(8)
 9300 RETURN
Crash the display on EO (Basic variables corrupt?)
MIRROR.P
(1.43 KiB) Downloaded 354 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
dr beep
Posts: 2076
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: binary mirroring? in basic

Post by dr beep »

10 input a$
20 let b$=""
30 for f= 1 to len a$
40 let b$=a$(f)+b$
50 next f
60 print a$,b$
solar penguin
Posts: 5
Joined: Wed Apr 19, 2017 8:29 pm

Re: binary mirroring? in basic

Post by solar penguin »

(* delurks *)

Hi, for completeness here's a BASIC binary mirroring routine. I know the OP probably doesn't need this any more, but I enjoyed the challenge.

Code: Select all

10 PRINT "INPUT WHOLE NUMBER 0 TO 255"
20 INPUT A
30 IF A<NOT PI OR A>VAL "255" OR A<>INT A THEN RUN
40 PRINT ,,A;" BINARY REVERSED IS"
100 LET B=NOT PI
110 FOR I=B TO VAL "7"
120 LET A=INT A/2
130 LET B=B+B+(A<>INT A)
140 NEXT I
200 PRINT B
The trick is the INT A/2 line 120 which really acts like (INT A)/2 even though I keep thinking it looks more like INT (A/2). This means it shifts A right with 0.5 at the end as a temporary carry flag. Line 130 shifts B the other direction including from the carry flag.
Post Reply