Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Zilog Z80
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Example code === The following Z80 assembly source code is for a subroutine named <code>memcpy</code> that copies a block of data bytes of a given size from one location to another. Important: the example code does not handle the case where the destination block overlaps the source; a serious limitation, but one that is irrelevant for some applications—such as, especially, when the source is in ROM and the destination in RAM, so they can never overlap. The data block is copied one byte at a time, and the data movement and looping logic utilizes 16-bit operations. It demonstrates a variety of instructions but in practice it would not be coded this way as the Z80 has a single instruction that will replace this entire subroutine: <code>LDIR</code>. The sample code will move one byte every 46 T-states. Substituting the <code>LDIR</code> instruction will move each byte in only 21 T-states. Note that the assembled code is binary-compatible with the Intel 8080 and 8085 CPUs. {| |-valign="top" |<syntaxhighlight lang="text" highlight="16"> 1000 1000 1000 F5 1001 7E 1002 12 1003 23 1004 13 1005 0B 1006 78 1007 B1 1008 C2 01 10 100B F1 100C C9 100D </syntaxhighlight> |<syntaxhighlight lang="tasm" highlight="16"> ; memcpy -- ; Copy a block of memory from one location to another. ; This routine is the equivalent of LDIR ; ; Entry registers ; HL - Address of source data block ; DE - Address of destination data block ; BC - Number of bytes to copy ; ; Return registers ; HL - First byte after source data block ; DE - First byte after destination data block ; BC - Zero ; (LDIR does not fully save AF. H, P/V, and N are reset.) org 1000h ; Origin at 1000h memcpy public push af ; Save AF like LDIR loop ld a,(hl) ; Copy 1 source byte ld (de),a ; to its destination inc hl ; Bump source pointer inc de ; Bump dest pointer dec bc ; Count the copied byte ld a,b ; Test BC for zero or c ; If BC != 0, jp nz,loop ; repeat the loop pop af ; Restore AF ret ; Return end </syntaxhighlight> |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)