Code: Select all
Gabriel Egan
Minimal Computing Lab
De Montfort University
Code: Select all
Code: Select all
zero
four
two
Code: Select all
; ***************************************************************************
; * BIT-FLIPPER Flip bits (7<>0, 6<>1, 5<>2, 4<>3) in each byte in a string *
; ***************************************************************************
; This program starts at 0100h
; The string starts at 8000h
; The stack is wherever CP/M puts it
; H,L holds the address where the string starts
; B,C holds the length of the string
; D holds a declining byte (from 10000000 to 01000000 to 00000001)
; that gets added to a number (stored in E) that that builds up the mirrored byte
; E stores the mirrored byte until we're ready to put it back into memory
org 0100h
LXI H, 8000h ; Put address of string-start into H,L pair
LXI B, 0005h ; Put string length (16 bits) into B,C pair
CALL NEXTBYTE ; Call the subroutine for mirroring
RET ; go back to CP/M
NEXTBYTE MVI E,0 ; Initialize E
MVI D,10000000b; Initialize pointer to the bit we're dealing with
NEXTBIT MOV A,M ; Load accum. with byte we're working on (pointed to by H,L)
RRC ; Pop off the rightmost bit to test
MOV M,A ; Put the rotated byte back in memory because we'll want to
; do that whether or not we popped a zero
JNC POPPEDZERO ; If the rightmost bit was zero, continue with next bit
MOV A,E ; Get the partially completed mirror-byte into Acc. so we can add to to it
ADD D ; Add the declining byte to it
MOV E,A ; Put the result back in E
POPPEDZERO MOV A,D ; Get the declining byte so we can rotate it
RRC ; Rotate the decline byte
MOV D,A ; Put the declining byte back into D
JNC NEXTBIT ; If a 0 popped out on the right side then D wasn't 00000001
; so there are more bits to do
MOV M,E ; if 1 popped we're done with this byte so write it to memory
; And let's work on the next byte
INX H ; Point to the next location in memory to work on
DCX B ; Lower pointer to number of bytes
MVI A,0 ; Compare C to 0 and . . .
CMP C ; . . . go back around loop if . . .
JNZ NEXTBYTE ; . . . there are more bytes to do
RET
Code: Select all
; ***************************************************************************
; * BIT-FLIPPER Flip bits (7<>0, 6<>1, 5<>2, 4<>3) in each byte in a string *
; ***************************************************************************
; This program starts at 0100h
; The string starts at 8000h
; The stack is wherever CP/M puts it
; H,L holds the address where the string starts
; B,C holds the length of the string
; D holds count of bits within a byte
; E stores the mirrored byte until we're ready to put it back into memory
org 0100h
LXI H,8000h ; put address of string-start into H,L pair
LXI B,0005h ; put string length (16 bits) into B,C pair
NXTBYTE MVI D,8 ; D=bit counter within a byte
NEXTBIT MOV A,M ; A=byte we're working on (pointed to by H,L)
RRC ; shift source byte right into carry
MOV M,A ; save the rotated byte back in memory
MOV A,E ; A=partially completed mirror byte
RAL ; shift new bit left into the mirror byte
MOV E,A ; save the temp result in E
DCR D ; decrement bit counter
JNZ NEXTBIT ; loop through all 8 bits
; The completely flipped byte is in A. Store back in memory at (HL),
; then increment HL, decrement byte count in BC until BC=0.
MOV M,A ; store mirrored byte in memory
INX H ; point to the next location in memory to work on
DCX B ; decrement 16-bit byte counter
MOV A,B ; A=msbyte of counter
ORA C ; A=msbyte + lsbyte of counter
JNZ NXTBYTE ; loop while BC not zero
RET ; return to CP/M
Users browsing this forum: Google [Bot] and 1 guest