<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://gbdev.gg8.se/wiki/index.php?action=history&amp;feed=atom&amp;title=SDCC_output_samples</id>
		<title>SDCC output samples - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://gbdev.gg8.se/wiki/index.php?action=history&amp;feed=atom&amp;title=SDCC_output_samples"/>
		<link rel="alternate" type="text/html" href="https://gbdev.gg8.se/wiki/index.php?title=SDCC_output_samples&amp;action=history"/>
		<updated>2026-05-01T10:51:45Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.1</generator>

	<entry>
		<id>https://gbdev.gg8.se/wiki/index.php?title=SDCC_output_samples&amp;diff=1051&amp;oldid=prev</id>
		<title>PinoBatch: memcpy turns into this?</title>
		<link rel="alternate" type="text/html" href="https://gbdev.gg8.se/wiki/index.php?title=SDCC_output_samples&amp;diff=1051&amp;oldid=prev"/>
				<updated>2022-01-16T16:58:38Z</updated>
		
		<summary type="html">&lt;p&gt;memcpy turns into this?&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;SDCC is a C compiler targeting, among other instruction sets, the Sharp SM83 CPU in the Game Boy. It is commonly used with the [[GBDK]]-2020 library to develop Game Boy games. Here are some samples of its output.&lt;br /&gt;
&lt;br /&gt;
== memcpy ==&lt;br /&gt;
C source code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void memcpy(char* dest, char* src, unsigned len) {&lt;br /&gt;
    while (len--) {&lt;br /&gt;
        *dest = *src;&lt;br /&gt;
        dest++;&lt;br /&gt;
        src++;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SDCC 4.1.0 compiles it to the following: ([https://godbolt.org/z/Evhxs6cqs View in Compiler Explorer])&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
_memcpy::&lt;br /&gt;
        add     sp, #-4&lt;br /&gt;
        ldhl    sp,     #6&lt;br /&gt;
        ld      a, (hl+)&lt;br /&gt;
        ld      e, (hl)&lt;br /&gt;
        ldhl    sp,     #0&lt;br /&gt;
        ld      (hl+), a&lt;br /&gt;
        ld      (hl), e&lt;br /&gt;
        ldhl    sp,     #8&lt;br /&gt;
        ld      a, (hl+)&lt;br /&gt;
        ld      e, (hl)&lt;br /&gt;
        ldhl    sp,     #2&lt;br /&gt;
        ld      (hl+), a&lt;br /&gt;
        ld      (hl), e&lt;br /&gt;
        ldhl    sp,#10&lt;br /&gt;
        ld      a, (hl+)&lt;br /&gt;
        ld      c, a&lt;br /&gt;
        ld      b, (hl)&lt;br /&gt;
00101$:&lt;br /&gt;
        ld      e, c&lt;br /&gt;
        ld      d, b&lt;br /&gt;
        dec     bc&lt;br /&gt;
        ld      a, d&lt;br /&gt;
        or      a, e&lt;br /&gt;
        jr      Z, 00104$&lt;br /&gt;
        ldhl    sp,#2&lt;br /&gt;
        ld      a, (hl+)&lt;br /&gt;
        ld      e, a&lt;br /&gt;
        ld      d, (hl)&lt;br /&gt;
        ld      a, (de)&lt;br /&gt;
        pop     hl&lt;br /&gt;
        push    hl&lt;br /&gt;
        ld      (hl), a&lt;br /&gt;
        ldhl    sp,     #0&lt;br /&gt;
        inc     (hl)&lt;br /&gt;
        jr      NZ, 00117$&lt;br /&gt;
        inc     hl&lt;br /&gt;
        inc     (hl)&lt;br /&gt;
00117$:&lt;br /&gt;
        ldhl    sp,     #2&lt;br /&gt;
        inc     (hl)&lt;br /&gt;
        jr      NZ, 00101$&lt;br /&gt;
        inc     hl&lt;br /&gt;
        inc     (hl)&lt;br /&gt;
        jr      00101$&lt;br /&gt;
00104$:&lt;br /&gt;
        add     sp, #4&lt;br /&gt;
        ret&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compare a handwritten implementation in assembly language that takes &amp;lt;code&amp;gt;dest&amp;lt;/code&amp;gt; in DE, &amp;lt;code&amp;gt;src&amp;lt;/code&amp;gt; in HL, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; in BC. (This is the same register allocation used for [[CPU Comparision with Z80|Zilog Z80]]'s &amp;lt;code&amp;gt;ldir&amp;lt;/code&amp;gt; instruction.)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
memcpy_loop:&lt;br /&gt;
        ld      a, (hl+)&lt;br /&gt;
        ld      (de), a&lt;br /&gt;
        dec     bc&lt;br /&gt;
_memcpy__de_hl_bc::&lt;br /&gt;
        ld      a, b&lt;br /&gt;
        or      a, c&lt;br /&gt;
        jr      nz, memcpy_loop&lt;br /&gt;
        ret&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>PinoBatch</name></author>	</entry>

	</feed>