1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Xorshift 16bit

Posted at

参考:Retro Programming: 16-Bit Xorshift Pseudorandom Numbers in Z80 Assembly

xors16.asm
; z88dk / WebMSX
; z80asm -b -l xors16.asm
; appmake +msx -b xors16.bin --org 0xd000 --fmsx

CHPUT   equ     $00a2

        org     $d000
start:
        ld      b, 10
.loop
        call    xrnd
        ld      de, buf
        ld      a, (xrnd+2)
        call    tohex2
        ld      a, (xrnd+1)
        call    tohex2
        ld      hl, buf
        call    puts
        djnz    loop
        ret

xrnd:
        ld      hl, 1

        ld      a, h
        rra
        ld      a, l
        rra
        xor     h
        ld      h, a
        ld      a, l
        rra
        ld      a, h
        rra
        xor     l
        ld      l, a
        xor     h
        ld      h, a

        ld      (xrnd+1), hl

        ret

puts:
        ld      a, (hl)
        or      a
        ret     z
        call    CHPUT
        inc     hl
        jr      puts

tohex2:
        push    af
        rlca
        rlca
        rlca
        rlca
        call    tohex
        pop     af
.tohex
        and     $0f
        cp      $0a
        sbc     $69
        daa
        ld      (de), a
        inc     de
        ret

buf:    defm    "xxxx", $0d, $0a, 0
xors16.asm
comment *
MASM32 SDK / MS-DOS
ml /c /AT /Fl xors16.asm
link16 /t xors16;
*
                .model  tiny
                .186

                .code
                org     0100h

main            proc
                mov     bx, 10
        @@loop:
                call    xrnd
                mov     di, offset buf
                call    tohex4
                mov     ah, 09h
                mov     dx, offset buf
                int     21h
                dec     bx
                jnz     @@loop

                mov     ax, 4c00h
                int     21h
main            endp

xrnd            proc
                mov     ax, seed
                mov     dx, ax                  ; ax ^= ax << 7
                shl     dx, 7
                xor     ax, dx
                mov     dx, ax                  ; ax ^= ax >> 9
                shr     dx, 9
                xor     ax, dx
                mov     dx, ax                  ; ax ^= ax << 8
                shl     dx, 8
                xor     ax, dx
                mov     seed, ax
                ret
xrnd            endp

tohex4          proc
                cld
                mov     cx, 4
        @@loop:
                rol     ax, 4
                push    ax
                and     al, 0fh
                cmp     al, 0ah
                sbb     al, 69h
                das
                stosb
                pop     ax
                loop    @@loop
                ret
tohex4          endp

seed            dw      1
buf             db      'xxxx', 0dh, 0ah, '$'

                end     main

WMSX Screen.png
xors16.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?