{"id":13,"date":"2022-07-22T15:44:44","date_gmt":"2022-07-22T20:44:44","guid":{"rendered":"https:\/\/fortycreek.org\/?p=13"},"modified":"2022-07-22T15:44:44","modified_gmt":"2022-07-22T20:44:44","slug":"sieve-of-eratosthenes-z80","status":"publish","type":"post","link":"https:\/\/fortycreek.org\/index.php\/2022\/07\/22\/sieve-of-eratosthenes-z80\/","title":{"rendered":"Sieve of Eratosthenes z80"},"content":{"rendered":"\n<p>I&#8217;ve been trying to learn z80 assembler, and one of the programs that I typically do for that is writing a version of the Sieve of Eratosthenes.  My version runs under CP\/M on my <a href=\"http:\/\/cpuville.com\/Kits\/Single-board-kit.html\">CPUVILLE<\/a> z80 computer.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>; this program implements the sieve of eratosthenes on the z80\nmonitor:        equ     046fh\ncpm:            equ     0fa00h\ndata_start:     equ     1000h\ndata_end:       equ     3000h\ncurrent_location:       equ     0x3000          ;word variable in RAM\nline_count:             equ     0x3002          ;byte variable in RAM\nbyte_count:             equ     0x3003          ;byte variable in RAM\nvalue_pointer:          equ     0x3004          ;word variable in RAM\ncurrent_value:          equ     0x3006          ;word variable in RAM\nbuffer:                 equ     0x3008          ;buffer in RAM -- up to stack area\nvar_c:  equ     0ffeh\n\norg 0100h  ; set origin of execution\n        ld de,data_start\n        ld a,00h\nzero_loop:\n        ld (de),a\n        inc de\n        ld hl,data_end\n        sbc hl,de\n        jp nz,zero_loop\n\n        ld de,02        ; start sieve at c=2\nmain_sieve_loop:        ; we are using de for this loop\n        ld (var_c),de   ; store in ram too\n        ld h,d          ; ld hl,de\n        ld l,e          ; use hl to figure out memory location\n        srl h           ; since we can store 8 bits in each memory loc\n        rr l            ; we need to shift left (16 bit) 3 times\n        srl h           ; https:\/\/chilliant.com\/z80shift.html\n        rr l            ; 2\n        srl h\n        rr l            ; 3\n        ld a,10h\n        add a,h         ; add 1000h to hl to get actual location\n        ld h,a          ; here is the data in (hl)\n        ld a,00000111b\n        and e           ; last 3 bits of de into a - will need srl a times\n        ld b,(hl)       ; load the data into b\n        cp 0b           ; compare a to 0\nshift_a_times_1:\n        jp z,done_shift_1\n        srl b\n        dec a\n        jp shift_a_times_1\ndone_shift_1:           ; a is garbage, b has the bit in the lsb position\n        ld a,00000001b\n        and b           ; Z flag will be correct here if zero\n        ld (var_c),de   ; store de in memory\n        jp nz,end_main_sieve_loop       ; non zero - just inc loop and get out\n; we found a zero, now need to loop and mark composites\n        ld h,d\n        ld l,e          ; ld hl,de\n        ld b,d\n        ld c,e\nstart_composite_loop:   ; de stores the value in this loop\n        add hl,bc       ; start at 2 * de to mark composites\n        jp c,end_main_sieve_loop        ; get out if overflow 16 bit\n        ld d,h\n        ld e,l          ; ld de,hl\n        srl h\n        rr l\n        srl h\n        rr l\n        srl h\n        rr l\n        ld a,10h\n        add a,h\n        ld h,a          ; here is the data in (hl)\n        ld a,00000111b\n        and e           ; a has which bit to set\n        ld b,00000001b  ; start with lsb\n        cp 0b           ; is a zero?\nshift_a_times_2:\n        jp z,done_shift_2\n        sla b\n        dec a\n        jp shift_a_times_2\ndone_shift_2:\n        ld a,(hl)       ; retreive what is in memory\n        or b            ; mark the bit\n        ld (hl),a       ; write back to memory\n        ld h,d\n        ld l,e          ; put hl back to where we were\n        ld bc,(var_c)   ; put step into bc\n        jp start_composite_loop\nend_main_sieve_loop:\n        ld de,(var_c)\n        inc de\n        jp nz,main_sieve_loop\n\n        ld hl,data_start\n        call memory_dump\n        ld hl,2f00h\n        call memory_dump\n\n        ret\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been trying to learn z80 assembler, and one of the programs that I typically do for that is writing a version of the Sieve of Eratosthenes. My version runs under CP\/M on my CPUVILLE z80 computer.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[8,7],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-z80","tag-sieve","tag-z80"],"_links":{"self":[{"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":1,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fortycreek.org\/index.php\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}