flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Tiny BitThin Binary Logic

Author
Thread Post new topic Reply to topic
mbr_tsr



Joined: 03 Apr 2011
Posts: 4903
mbr_tsr
Code:
;===============================================================
;Tiny BitThin Binary Logic - Create your own Instructions
;Version 1.0 by MBR_TSR
;===============================================================
;
;b1=byte#1 (AH) 00000000
;b2=byte#2 (AL) 00000000
;b3=byte#3 (BL) 00000000
;x=0 or 1
;y=0 or 1
;z=0 or 1
;bn=bit# (0 to 7)
;
;if b1 bn is x and b2 bn is y then set b3 bn to z
;if any b1 bn is x and any b2 bn is y set b3 bn to z
;
;===============================================================
;MagicByte1 = 00001010
;MagicByte2 = 00001100
;===============================================================

Org 100h

push cs
push cs
pop ds
pop es

mov si, Intro
call Show
mov si, CrLf
call Show

mov di, LogicArray+0
call SetMagic
and ah, al
mov al, ah
stosb

mov di, LogicArray+1
call SetMagic
or ah, al
mov al, ah
stosb

mov di, LogicArray+2
call SetMagic
and ah, al
and ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+3
call SetMagic
or ah, al
or ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+4
call SetMagic
xor ah, al
mov al, ah
stosb

mov si, Msg1
call Show
call SetMagic
xchg ah, al
mov si, JunkStr
call Byte2BinStr
call Show

mov si, CrLf
call Show

mov si, Msg2
call Show
call SetMagic
mov si, JunkStr
call Byte2BinStr
call Show

mov si, LogicArray+0
lodsb
mov si, JunkStr
call Byte2BinStr
mov si, Logic0
call Show
call ShowJunk

mov si, LogicArray+1
lodsb
mov si, JunkStr
call Byte2BinStr
mov si, Logic1
call Show
call ShowJunk

mov si, LogicArray+2
lodsb
mov si, JunkStr
call Byte2BinStr
mov si, Logic2
call Show
call ShowJunk

mov si, LogicArray+3
lodsb
mov si, JunkStr
call Byte2BinStr
mov si, Logic3
call Show
call ShowJunk

mov si, LogicArray+4
lodsb
mov si, JunkStr
call Byte2BinStr
mov si, Logic4
call Show
call ShowJunk

mov si, CrLf
call Show

int 20h

Byte1 db 00000000b
Byte2 db 00000000b
Byte3 db 00000000b
Byte1Str db "00000000",0
Byte2Str db "00000000",0
Byte3Str db "00000000",0
JunkStr db "00000000",0

Magic1 db 00001010b
Magic2 db 00001100b

LogicArray rb 5

CrLf db 13,10,0
Msg1 db "AH=",0
Msg2 db "AL=",0
Logic0 db 13,10,"ah and al  =",0
Logic1 db 13,10,"ah or al   =",0
Logic2 db 13,10,"ah nand al =",0
Logic3 db 13,10,"ah nor al  =",0
Logic4 db 13,10,"ah xor al  =",0

SetMagic:
mov ah, byte [cs:Magic1]
mov al, byte [cs:Magic2]
ret

ShowJunk:
mov si, JunkStr
call Show
ret

Intro:
db 13,10,"Tiny BitThin Binary Logic Demo",0

;===============================================================
;Convert number in AL to a string of zeros and ones
;AL=number Returns DS:SI=binary string

Byte2BinStr:
push ax
push di
push es
push ds
pop es
mov di, si
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, 0
stosb
pop es
pop di
pop ax
push bx
mov bh, ah
mov ah, al
and al, 00000001b
cmp al, 00000001b
jne NB0
mov byte [ds:si+7], '1'
NB0:
mov al, ah
and al, 00000010b
cmp al, 00000010b
jne NB1
mov byte [ds:si+6], '1'
NB1:
mov al, ah
and al, 00000100b
cmp al, 00000100b
jne NB2
mov byte [ds:si+5], '1'
NB2:
mov al, ah
and al, 00001000b
cmp al, 00001000b
jne NB3
mov byte [ds:si+4], '1'
NB3:
mov al, ah
and al, 00010000b
cmp al, 00010000b
jne NB4
mov byte [ds:si+3], '1'
NB4:
mov al, ah
and al, 00100000b
cmp al, 00100000b
jne NB5
mov byte [ds:si+2], '1'
NB5:
mov al, ah
and al, 01000000b
cmp al, 01000000b
jne NB6
mov byte [ds:si+1], '1'
NB6:
mov al, ah
and al, 10000000b
cmp al, 10000000b
jne NB7
mov byte [ds:si+0], '1'
NB7:
mov ah, bh
pop bx
ret
;===============================================================
;Convert string of zeros and ones to number in AL
;DS:SI=binary string Returns AL=number

BinStr2Asc:
mov  al, 0
cmp  byte [ds:si+0], '1'
jne  NotB0
add  al, 10000000b
NotB0:
cmp  byte [ds:si+1], '1'
jne  NotB1
add  al, 01000000b
NotB1:
cmp  byte [ds:si+2], '1'
jne  NotB2
add  al, 00100000b
NotB2:
cmp  byte [ds:si+3], '1'
jne  NotB3
add  al, 00010000b
NotB3:
cmp  byte [ds:si+4], '1'
jne  NotB4
add  al, 00001000b
NotB4:
cmp  byte [ds:si+5], '1'
jne  NotB5
add  al, 00000100b
NotB5:
cmp  byte [ds:si+6], '1'
jne  NotB6
add  al, 00000010b
NotB6:
cmp  byte [ds:si+7], '1'
jne  NotB7
add  al, 00000001b
NotB7:
ret
;===============================================================
;Display NUL Terminated String in DS:SI
Show:
push ax
push bx
push si
Show1:
lodsb
cmp  al, 0
je   Okay
mov  ah, 0x0E
mov  bh, 0x00
mov  bl, 0x07
int  0x10
jmp  Show1
Okay:
pop  si
pop  bx
pop  ax
ret
;===============================================================

    
Post 15 Jun 2013, 02:06
View user's profile Send private message Reply with quote
mbr_tsr



Joined: 03 Apr 2011
Posts: 4903
mbr_tsr
Code:
;===============================================================
;Tiny BitThin Binary Logic - Create your own Instructions
;Version 2.0 by MBR_TSR
;===============================================================
;
;b1=byte#1 (AH) 00000000
;b2=byte#2 (AL) 00000000
;b3=byte#3 (BL) 00000000
;x=0 or 1
;y=0 or 1
;z=0 or 1
;bn=bit# (0 to 7)
;
;if b1 bn is x and b2 bn is y then set b3 bn to z
;if any b1 bn is x and any b2 bn is y set b3 bn to z
;
;===============================================================
;MagicByte1 = 00001010
;MagicByte2 = 00001100
;===============================================================

Org 100h

push cs
push cs
pop ds
pop es

mov si, Intro
call Show
mov si, CrLf
call Show

mov si, EnterAX
call Show
call GetAXasHex
xchg ah, al
mov di, Magic1
stosw

mov si, CrLf
call Show

mov di, LogicArray+0
call SetMagic
and ah, al
mov al, ah
stosb

mov di, LogicArray+1
call SetMagic
or ah, al
mov al, ah
stosb

mov di, LogicArray+2
call SetMagic
and ah, al
and ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+3
call SetMagic
or ah, al
or ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+4
call SetMagic
xor ah, al
mov al, ah
stosb

mov si, Msg1
call Show
call SetMagic
xchg ah, al
mov si, JunkStr
call Byte2BinStr
call Show

mov si, MsgB
call Show
call SetMagic
xchg ah, al
call PrintALAsHex

call SetMagic
xchg ah, al
mov ah, 0
call GetPrintDecStr

mov si, CrLf
call Show

mov si, Msg2
call Show
call SetMagic
mov si, JunkStr
call Byte2BinStr
call Show

mov si, MsgB
call Show
call SetMagic
call PrintALAsHex

call SetMagic
mov ah, 0
call GetPrintDecStr

mov si, LogicArray+0
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic0
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+1
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic1
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+2
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic2
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+3
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic3
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+4
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic4
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

call MyInstruction
mov si, Logic5
call Show
mov si, Byte3Str
call Show
mov si, MsgB
call Show
mov si, Byte3Str
call BinStr2Asc
mov ah, 0
call PrintALAsHex
call GetPrintDecStr

mov si, CrLf
call Show

int 20h

Byte1 db 00000000b
Byte2 db 00000000b
Byte3 db 00000000b
Byte1Str db "00000000",0
Byte2Str db "00000000",0
Byte3Str db "00000000",0
JunkStr db "00000000",0
DecimalStr db "00000",0

Magic1 db 00001010b
Magic2 db 00001100b

LogicArray rb 5

EnterAX:
db 13,10,"01=Low 7F=Medium 0xFF=High"
db 13,10,"01=Start 7F=During 0xFF=After"
db 13,10,"Enter AX as Hex?",0

CrLf db 13,10,0
Msg1 db "AH=",0
Msg2 db "AL=",0
MsgB db " (hex)=",0
MsgC db " Decimal=",0
Logic0 db 13,10,"ah and al  =",0
Logic1 db 13,10,"ah or al   =",0
Logic2 db 13,10,"ah nand al =",0
Logic3 db 13,10,"ah nor al  =",0
Logic4 db 13,10,"ah xor al  =",0
Logic5 db 13,10,"ah zdiv al =",0

SetMagic:
mov ah, byte [ds:Magic1]
mov al, byte [ds:Magic2]
ret

ShowJunk:
mov si, JunkStr
call Show
ret

GetPrintDecStr:
mov si, MsgC
call Show
mov si, DecimalStr
call Hex2DecStr
call Show
ret

Intro:
db 13,10,"Tiny BitThin Binary Logic Demo",0

;===============================================================
;Convert number in AL to a string of zeros and ones
;AL=number Returns DS:SI=binary string

Byte2BinStr:
push ax
push di
push es
push ds
pop es
mov di, si
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, 0
stosb
pop es
pop di
pop ax
push bx
mov bh, ah
mov ah, al
and al, 00000001b
cmp al, 00000001b
jne NB0
mov byte [ds:si+7], '1'
NB0:
mov al, ah
and al, 00000010b
cmp al, 00000010b
jne NB1
mov byte [ds:si+6], '1'
NB1:
mov al, ah
and al, 00000100b
cmp al, 00000100b
jne NB2
mov byte [ds:si+5], '1'
NB2:
mov al, ah
and al, 00001000b
cmp al, 00001000b
jne NB3
mov byte [ds:si+4], '1'
NB3:
mov al, ah
and al, 00010000b
cmp al, 00010000b
jne NB4
mov byte [ds:si+3], '1'
NB4:
mov al, ah
and al, 00100000b
cmp al, 00100000b
jne NB5
mov byte [ds:si+2], '1'
NB5:
mov al, ah
and al, 01000000b
cmp al, 01000000b
jne NB6
mov byte [ds:si+1], '1'
NB6:
mov al, ah
and al, 10000000b
cmp al, 10000000b
jne NB7
mov byte [ds:si+0], '1'
NB7:
mov ah, bh
pop bx
ret
;===============================================================
;Convert string of zeros and ones to number in AL
;DS:SI=binary string Returns AL=number

BinStr2Asc:
mov  al, 0
cmp  byte [ds:si+0], '1'
jne  NotB0
add  al, 10000000b
NotB0:
cmp  byte [ds:si+1], '1'
jne  NotB1
add  al, 01000000b
NotB1:
cmp  byte [ds:si+2], '1'
jne  NotB2
add  al, 00100000b
NotB2:
cmp  byte [ds:si+3], '1'
jne  NotB3
add  al, 00010000b
NotB3:
cmp  byte [ds:si+4], '1'
jne  NotB4
add  al, 00001000b
NotB4:
cmp  byte [ds:si+5], '1'
jne  NotB5
add  al, 00000100b
NotB5:
cmp  byte [ds:si+6], '1'
jne  NotB6
add  al, 00000010b
NotB6:
cmp  byte [ds:si+7], '1'
jne  NotB7
add  al, 00000001b
NotB7:
ret
;===============================================================
PrintALAsHex:
push AX
push BX
push CX
MOV  CL,10h
MOV  AH,0
DIV  CL
ADD  AL,30h
ADD  AH,30h
CMP  AL,'9'
JBE  CA4B
ADD  AL,7
CA4B:
CMP  AH,'9'
JBE  CA5B
ADD  AH,7
CA5B:
mov  CX,AX
mov  al,CL
mov  ah, 0eh
mov  bh, 0
mov  bl, 7
int  10h
mov  al,CH
mov  ah, 0eh
mov  bh, 0
int  10h
pop  CX
pop  BX
pop  AX
ret
;===============================================================
;Display NUL Terminated String in DS:SI
Show:
push ax
push bx
push si
Show1:
lodsb
cmp  al, 0
je   Okay
mov  ah, 0x0E
mov  bh, 0x00
mov  bl, 0x07
int  0x10
jmp  Show1
Okay:
pop  si
pop  bx
pop  ax
ret
;===============================================================
GetAXasHex:
push cx
push dx
push es
mov  ah, 0x03
mov  bh, 0
int  0x10
ReDoItz:
mov  ah, 0x02
mov  bh, 0
int  0x10
mov  ah, 0x0e
mov  bx, 0x0007
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  ah, 0x02
mov  bh, 0
int  0x10
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  ch, al
mov  ah, 0x0e
int  0x10
xchg ah, al
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  cl, al
mov  ah, 0x0e
int  0x10
mov  es, cx
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  ch, al
mov  ah, 0x0e
int  0x10
xchg ah, al
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  cl, al
mov  ah, 0x0e
int  0x10
mov  ax, es
mov  bx, cx
pop  es
pop  dx
pop  cx
push dx
call hax 
mov dh, al
mov ax, bx
call hax
mov dl, al
mov ax, dx
pop dx
ret
;===============================================================
hax:
push cx
push dx
mov  cl,4
xor  dx,dx
hscn:
xchg ah, al
cmp  al,'0'
jb   hendr
cmp  al,'9'
jna  hdecdgt
or   al,0x20
cmp  al,'a'
jb   hendr
cmp  al,'z'
ja   hendr
sub  al,'a'-'9'-1
hdecdgt:
sub  al,'0'
shl  dx,cl
or   dl,al
jmp  hscn
hendr:
xchg dx, ax
dec  si
pop  dx
pop  cx
ret
;===============================================================
;Hex -> Decimal Call With AX=Number Returns DS:SI=Decimal String
Hex2DecStr:
push cx
mov byte [cs:AX2DEC+4], '0'
mov byte [cs:AX2DEC+3], '0'
mov byte [cs:AX2DEC+2], '0'
mov byte [cs:AX2DEC+1], '0'
mov byte [cs:AX2DEC+0], '0'
mov cx, ax
cmp cx, 0
je DoneCnt
inc cx
CountUp:
KeepCount:
cmp byte [cs:AX2DEC+4], ':'
je Byte4is10
cmp byte [cs:AX2DEC+3], ':'
je Byte3is10
cmp byte [cs:AX2DEC+2], ':'
je Byte2is10
cmp byte [cs:AX2DEC+1], ':'
je Byte1is10
jmp NoNines
Byte4is10:
mov byte [cs:AX2DEC+4], '0'
inc byte [cs:AX2DEC+3]
jmp KeepCount
Byte3is10:
mov byte [cs:AX2DEC+3], '0'
inc byte [cs:AX2DEC+2]
jmp KeepCount
Byte2is10:
mov byte [cs:AX2DEC+2], '0'
inc byte [cs:AX2DEC+1]
jmp KeepCount
Byte1is10:
mov byte [cs:AX2DEC+1], '0'
inc byte [cs:AX2DEC+0]
jmp KeepCount
NoNines:
cmp cx, 1
je Skip10
inc byte [cs:AX2DEC+4] 
Skip10:
loop CountUp
DoneCnt:
pop cx
push cs
pop ds
mov si, AX2DEC
ret
AX2DEC db "00000",0
;===============================================================
;       x=00001010b
;       y=00001100b
;x zdiv y=00001101b
MyInstruction:
mov al, byte [ds:Magic1]
mov si, Byte1Str
call Byte2BinStr
mov al, byte [ds:Magic2]
mov si, Byte2Str
call Byte2BinStr
mov cx, 8
mov bx, 0
MyInstructionLoop:
mov ah, byte [ds:Byte1Str+BX]
mov al, byte [ds:Byte2Str+BX]
cmp ah, '0'
jne Byte1BitNot0
 cmp al, '0'
 je SetByte3ToBit
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
 SetByte3ToBit:
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
Byte1BitNot0:
 cmp al, '1'
 je SetByte3To1B
 mov byte [ds:Byte3Str+BX], '0'
 jmp ByteSetA
 SetByte3To1B:
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
 ByteSetA:
inc BX
loop MyInstructionLoop
ret


    
Post 15 Jun 2013, 04:32
View user's profile Send private message Reply with quote
mbr_tsr



Joined: 03 Apr 2011
Posts: 4903
mbr_tsr
Code:
;===============================================================
;Tiny BitThin Binary Logic - Create your own Instructions
;Version 3.0 by MBR_TSR
;===============================================================
;
;b1=byte#1 (AH) 00000000
;b2=byte#2 (AL) 00000000
;b3=byte#3 (BL) 00000000
;x=0 or 1
;y=0 or 1
;z=0 or 1
;bn=bit# (0 to 7)
;
;if b1 bn is x and b2 bn is y then set b3 bn to z
;if any b1 bn is x and any b2 bn is y set b3 bn to z
;
;===============================================================
;MagicByte1 = 00001010
;MagicByte2 = 00001100
;===============================================================

Org 100h

jmp SkipHeader
;Create your own Instruction Here (mine)
;Simply change '0' or '1' to create 'mine' instruction.
db 'BitThin$'
LowLow   db '0' ;Low Low
LowHigh  db '0' ;Low High
HighLow  db '0' ;High Low
HighHigh db '0' ;High High
SkipHeader:

push cs
push cs
pop ds
pop es

mov si, Intro
call Show
mov si, CrLf
call Show

mov si, EnterAX
call Show
call GetAXasHex
xchg ah, al
mov di, Magic1
stosw

mov si, CrLf
call Show

mov di, LogicArray+0
call SetMagic
and ah, al
mov al, ah
stosb

mov di, LogicArray+1
call SetMagic
or ah, al
mov al, ah
stosb

mov di, LogicArray+2
call SetMagic
and ah, al
and ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+3
call SetMagic
or ah, al
or ah, al
not ah
mov al, ah
stosb

mov di, LogicArray+4
call SetMagic
xor ah, al
mov al, ah
stosb

mov si, Msg1
call Show
call SetMagic
xchg ah, al
mov si, JunkStr
call Byte2BinStr
call Show

mov si, MsgB
call Show
call SetMagic
xchg ah, al
call PrintALAsHex

call SetMagic
xchg ah, al
mov ah, 0
call GetPrintDecStr

mov si, CrLf
call Show

mov si, Msg2
call Show
call SetMagic
mov si, JunkStr
call Byte2BinStr
call Show

mov si, MsgB
call Show
call SetMagic
call PrintALAsHex

call SetMagic
mov ah, 0
call GetPrintDecStr

mov si, LogicArray+0
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic0
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+1
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic1
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+2
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic2
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+3
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic3
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

mov si, LogicArray+4
lodsb
mov bl, al
mov si, JunkStr
call Byte2BinStr
mov si, Logic4
call Show
call ShowJunk
mov si, MsgB
call Show
mov ah, 0
mov al, bl
call PrintALAsHex
call GetPrintDecStr

call MyInstruction
mov si, Logic5
call Show
mov si, Byte3Str
call Show
mov si, MsgB
call Show
mov si, Byte3Str
call BinStr2Asc
mov ah, 0
call PrintALAsHex
call GetPrintDecStr

call MyOwnInstruction
mov si, Logic6
call Show
mov si, Byte3Str
call Show
mov si, MsgB
call Show
mov si, Byte3Str
call BinStr2Asc
mov ah, 0
call PrintALAsHex
call GetPrintDecStr

mov si, CrLf
call Show

int 20h

Byte1 db 00000000b
Byte2 db 00000000b
Byte3 db 00000000b
Byte1Str db "00000000",0
Byte2Str db "00000000",0
Byte3Str db "00000000",0
JunkStr db "00000000",0
DecimalStr db "00000",0

Magic1 db 00001010b
Magic2 db 00001100b

LogicArray rb 5

EnterAX:
db 13,10,"01=Low 7F=Medium FF=High"
db 13,10,"01=Start 7F=During FF=After"
db 13,10,"Magic number is 0A0C"
db 13,10,"Enter AX as Hex?",0

CrLf db 13,10,0
Msg1 db "AH=",0
Msg2 db "AL=",0
MsgB db " Hex=",0
MsgC db " Decimal=",0
Logic0 db 13,10,"AH and AL  =",0
Logic1 db 13,10,"AH or AL   =",0
Logic2 db 13,10,"AH nand AL =",0
Logic3 db 13,10,"AH nor AL  =",0
Logic4 db 13,10,"AH xor AL  =",0
Logic5 db 13,10,"AH zdiv AL =",0
Logic6 db 13,10,"AH mine AL =",0

SetMagic:
mov ah, byte [ds:Magic1]
mov al, byte [ds:Magic2]
ret

ShowJunk:
mov si, JunkStr
call Show
ret

GetPrintDecStr:
mov si, MsgC
call Show
mov si, DecimalStr
call Hex2DecStr
call Show
ret

Intro:
db 13,10,"Tiny BitThin Binary Logic Demo",0

;===============================================================
;Convert number in AL to a string of zeros and ones
;AL=number Returns DS:SI=binary string

Byte2BinStr:
push ax
push di
push es
push ds
pop es
mov di, si
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, '0'
stosb
mov al, 0
stosb
pop es
pop di
pop ax
push bx
mov bh, ah
mov ah, al
and al, 00000001b
cmp al, 00000001b
jne NB0
mov byte [ds:si+7], '1'
NB0:
mov al, ah
and al, 00000010b
cmp al, 00000010b
jne NB1
mov byte [ds:si+6], '1'
NB1:
mov al, ah
and al, 00000100b
cmp al, 00000100b
jne NB2
mov byte [ds:si+5], '1'
NB2:
mov al, ah
and al, 00001000b
cmp al, 00001000b
jne NB3
mov byte [ds:si+4], '1'
NB3:
mov al, ah
and al, 00010000b
cmp al, 00010000b
jne NB4
mov byte [ds:si+3], '1'
NB4:
mov al, ah
and al, 00100000b
cmp al, 00100000b
jne NB5
mov byte [ds:si+2], '1'
NB5:
mov al, ah
and al, 01000000b
cmp al, 01000000b
jne NB6
mov byte [ds:si+1], '1'
NB6:
mov al, ah
and al, 10000000b
cmp al, 10000000b
jne NB7
mov byte [ds:si+0], '1'
NB7:
mov ah, bh
pop bx
ret
;===============================================================
;Convert string of zeros and ones to number in AL
;DS:SI=binary string Returns AL=number

BinStr2Asc:
mov  al, 0
cmp  byte [ds:si+0], '1'
jne  NotB0
add  al, 10000000b
NotB0:
cmp  byte [ds:si+1], '1'
jne  NotB1
add  al, 01000000b
NotB1:
cmp  byte [ds:si+2], '1'
jne  NotB2
add  al, 00100000b
NotB2:
cmp  byte [ds:si+3], '1'
jne  NotB3
add  al, 00010000b
NotB3:
cmp  byte [ds:si+4], '1'
jne  NotB4
add  al, 00001000b
NotB4:
cmp  byte [ds:si+5], '1'
jne  NotB5
add  al, 00000100b
NotB5:
cmp  byte [ds:si+6], '1'
jne  NotB6
add  al, 00000010b
NotB6:
cmp  byte [ds:si+7], '1'
jne  NotB7
add  al, 00000001b
NotB7:
ret
;===============================================================
PrintALAsHex:
push AX
push BX
push CX
MOV  CL,10h
MOV  AH,0
DIV  CL
ADD  AL,30h
ADD  AH,30h
CMP  AL,'9'
JBE  CA4B
ADD  AL,7
CA4B:
CMP  AH,'9'
JBE  CA5B
ADD  AH,7
CA5B:
mov  CX,AX
mov  al,CL
mov  ah, 0eh
mov  bh, 0
mov  bl, 7
int  10h
mov  al,CH
mov  ah, 0eh
mov  bh, 0
int  10h
pop  CX
pop  BX
pop  AX
ret
;===============================================================
;Display NUL Terminated String in DS:SI
Show:
push ax
push bx
push si
Show1:
lodsb
cmp  al, 0
je   Okay
mov  ah, 0x0E
mov  bh, 0x00
mov  bl, 0x07
int  0x10
jmp  Show1
Okay:
pop  si
pop  bx
pop  ax
ret
;===============================================================
GetAXasHex:
push cx
push dx
push es
mov  ah, 0x03
mov  bh, 0
int  0x10
ReDoItz:
mov  ah, 0x02
mov  bh, 0
int  0x10
mov  ah, 0x0e
mov  bx, 0x0007
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  al, '0'
int  0x10
mov  ah, 0x02
mov  bh, 0
int  0x10
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  ch, al
mov  ah, 0x0e
int  0x10
xchg ah, al
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  cl, al
mov  ah, 0x0e
int  0x10
mov  es, cx
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  ch, al
mov  ah, 0x0e
int  0x10
xchg ah, al
mov  ah, 0
int  0x16
cmp  al, 8
je   ReDoItz
cmp  al, 0
je   ReDoItz
mov  cl, al
mov  ah, 0x0e
int  0x10
mov  ax, es
mov  bx, cx
pop  es
pop  dx
pop  cx
push dx
call hax 
mov dh, al
mov ax, bx
call hax
mov dl, al
mov ax, dx
pop dx
ret
;===============================================================
hax:
push cx
push dx
mov  cl,4
xor  dx,dx
hscn:
xchg ah, al
cmp  al,'0'
jb   hendr
cmp  al,'9'
jna  hdecdgt
or   al,0x20
cmp  al,'a'
jb   hendr
cmp  al,'z'
ja   hendr
sub  al,'a'-'9'-1
hdecdgt:
sub  al,'0'
shl  dx,cl
or   dl,al
jmp  hscn
hendr:
xchg dx, ax
dec  si
pop  dx
pop  cx
ret
;===============================================================
;Hex -> Decimal Call With AX=Number Returns DS:SI=Decimal String
Hex2DecStr:
push cx
mov byte [cs:AX2DEC+4], '0'
mov byte [cs:AX2DEC+3], '0'
mov byte [cs:AX2DEC+2], '0'
mov byte [cs:AX2DEC+1], '0'
mov byte [cs:AX2DEC+0], '0'
mov cx, ax
cmp cx, 0
je DoneCnt
inc cx
CountUp:
KeepCount:
cmp byte [cs:AX2DEC+4], ':'
je Byte4is10
cmp byte [cs:AX2DEC+3], ':'
je Byte3is10
cmp byte [cs:AX2DEC+2], ':'
je Byte2is10
cmp byte [cs:AX2DEC+1], ':'
je Byte1is10
jmp NoNines
Byte4is10:
mov byte [cs:AX2DEC+4], '0'
inc byte [cs:AX2DEC+3]
jmp KeepCount
Byte3is10:
mov byte [cs:AX2DEC+3], '0'
inc byte [cs:AX2DEC+2]
jmp KeepCount
Byte2is10:
mov byte [cs:AX2DEC+2], '0'
inc byte [cs:AX2DEC+1]
jmp KeepCount
Byte1is10:
mov byte [cs:AX2DEC+1], '0'
inc byte [cs:AX2DEC+0]
jmp KeepCount
NoNines:
cmp cx, 1
je Skip10
inc byte [cs:AX2DEC+4] 
Skip10:
loop CountUp
DoneCnt:
pop cx
push cs
pop ds
mov si, AX2DEC
ret
AX2DEC db "00000",0
;===============================================================
;       x=00001010b
;       y=00001100b
;x zdiv y=00001101b
MyInstruction:
mov al, byte [ds:Magic1]
mov si, Byte1Str
call Byte2BinStr
mov al, byte [ds:Magic2]
mov si, Byte2Str
call Byte2BinStr
mov cx, 8
mov bx, 0
MyInstructionLoop:
mov ah, byte [ds:Byte1Str+BX]
mov al, byte [ds:Byte2Str+BX]
cmp ah, '0'
jne Byte1BitNot0
 cmp al, '0'
 je SetByte3ToBit
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
 SetByte3ToBit:
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
Byte1BitNot0:
 cmp al, '1'
 je SetByte3To1B
 mov byte [ds:Byte3Str+BX], '0'
 jmp ByteSetA
 SetByte3To1B:
 mov byte [ds:Byte3Str+BX], '1'
 jmp ByteSetA
 ByteSetA:
inc BX
loop MyInstructionLoop
ret

;===============================================================
MyOwnInstruction:
mov al, byte [ds:Magic1]
mov si, Byte1Str
call Byte2BinStr
mov al, byte [ds:Magic2]
mov si, Byte2Str
call Byte2BinStr
mov cx, 8
mov bx, 0
MyInstructionLoopOwn:
mov ah, byte [ds:Byte1Str+BX]
mov al, byte [ds:Byte2Str+BX]
cmp ah, '0'
jne Byte1BitNot0Own
 cmp al, '0'
 je SetByte3ToBitOwn
 mov dl, byte [cs:LowHigh]
 mov byte [ds:Byte3Str+BX], dl
 jmp ByteSetAOwn
 SetByte3ToBitOwn:
 mov dl, byte [cs:LowLow]
 mov byte [ds:Byte3Str+BX], dl
 jmp ByteSetAOwn
Byte1BitNot0Own:
 cmp al, '1'
 je SetByte3To1BOwn
 mov dl, byte [cs:HighLow]
 mov byte [ds:Byte3Str+BX], dl
 jmp ByteSetAOwn
 SetByte3To1BOwn:
 mov dl, byte [cs:HighHigh]
 mov byte [ds:Byte3Str+BX], dl
 jmp ByteSetAOwn
 ByteSetAOwn:
inc BX
loop MyInstructionLoopOwn
ret


    
Post 15 Jun 2013, 20:24
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.