RosettaCodeData/Task/Gray-code/NOWUT/gray-code.nowut

65 lines
1.6 KiB
Plaintext

; link with PIOxxx.OBJ
sectiondata
output: db " : "
inbinary: db "00000 => "
graybinary: db "00000 => "
outbinary: db "00000"
db 13,10,0 ; carriage return and null terminator
sectioncode
start!
gosub initplatform
beginfunc
localvar i.d,g.d,b.d
i=0
whileless i,32
callex g,gray_encode,i
callex b,gray_decode,g
callex ,bin2string,i,inbinary,5 ; 5 = number of binary digits
callex ,bin2string,g,graybinary,5
callex ,bin2string,b,outbinary,5
callex ,printhex8,i ; display hex value
; because there is no PIO routine for decimals...
callex ,printnt,output.a
i=_+1
wend
endfunc
end
gray_encode:
beginfunc n.d
n=_ xor (n shr 1)
endfunc n
returnex 4 ; clean off 1 parameter from the stack
gray_decode:
beginfunc n.d
localvar p.d
p=n
whilegreater n,1
n=_ shr 1 > p=_ xor n
wend
endfunc p
returnex 4 ; clean off 1 parameter from the stack
bin2string:
beginfunc digits.d,straddr.d,value.d
whilegreater digits,0
digits=_-1
[straddr].b=value shr digits and 1+$30 ; write an ASCII '0' or '1'
straddr=_+1 ; increment the pointer
wend
endfunc
returnex $0C ; clean off 3 parameters from the stack