Files
damn_simple_architecture/book/dsa/instructions/hardware.html
T
2025-06-27 18:30:26 +01:00

398 lines
22 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Hardware Instructions - damn_simple_architecture</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../../";
const default_light_theme = "light";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">damn_simple_architecture</h1>
<div class="right-buttons">
<a href="../../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="hardware-instructions"><a class="header" href="#hardware-instructions">Hardware Instructions</a></h1>
<h3 id="data-movement-instructions"><a class="header" href="#data-movement-instructions">Data Movement Instructions</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>MOV</strong></td><td><code>src_reg, dest_reg</code></td><td>Copy value from source to destination register</td></tr>
<tr><td><strong>MOVS</strong></td><td><code>src_reg, dest_reg</code></td><td>Copy with sign extension</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">mov rg0, rg1 ; Copy rg0 to rg1
movs rg0, rg1 ; Copy rg0 to rg1 with sign extension
</code></pre>
<h3 id="memory-access-instructions"><a class="header" href="#memory-access-instructions">Memory Access Instructions</a></h3>
<h4 id="load-instructions"><a class="header" href="#load-instructions">Load Instructions</a></h4>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>LDB</strong></td><td><code>base_reg, dest_reg [, offset]</code><br><code>label, dest_reg [, offset]</code></td><td>Load byte from memory</td></tr>
<tr><td><strong>LDBS</strong></td><td><code>base_reg, dest_reg [, offset]</code><br><code>label, dest_reg [, offset]</code></td><td>Load byte with sign extension</td></tr>
<tr><td><strong>LDH</strong></td><td><code>base_reg, dest_reg [, offset]</code><br><code>label, dest_reg [, offset]</code></td><td>Load half-word (16-bit)</td></tr>
<tr><td><strong>LDHS</strong></td><td><code>base_reg, dest_reg [, offset]</code><br><code>label, dest_reg [, offset]</code></td><td>Load half-word with sign extension</td></tr>
<tr><td><strong>LDW</strong></td><td><code>base_reg, dest_reg [, offset]</code><br><code>label, dest_reg [, offset]</code></td><td>Load word (32-bit)</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">; Direct register addressing
ldb rg0, rg1 ; Load byte from address in rg0
ldw rg0, rg1, 8 ; Load word from (rg0 + 8)
; Label addressing
ldb buffer, rg2 ; Load byte from label 'buffer'
ldw stack, bpr ; Load stack address into base pointer
</code></pre>
<p><strong>Label Expansions:</strong></p>
<pre><code class="language-dsa">; ldb buffer, rg2 expands to:
lli buffer, rg2 ; Load lower 16 bits of buffer address
lui buffer, rg2 ; Load upper 16 bits of buffer address
ldb rg2, rg2 ; Load byte from address in rg2
; ldw stack, bpr expands to:
lli stack, bpr ; Load lower 16 bits of stack address
lui stack, bpr ; Load upper 16 bits of stack address
ldw bpr, bpr ; Load word from address in bpr
</code></pre>
<h4 id="store-instructions"><a class="header" href="#store-instructions">Store Instructions</a></h4>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>STB</strong></td><td><code>src_reg, base_reg [, offset]</code><br><code>src_reg, label [, offset]</code></td><td>Store byte to memory</td></tr>
<tr><td><strong>STH</strong></td><td><code>src_reg, base_reg [, offset]</code><br><code>src_reg, label [, offset]</code></td><td>Store half-word to memory</td></tr>
<tr><td><strong>STW</strong></td><td><code>src_reg, base_reg [, offset]</code><br><code>src_reg, label [, offset]</code></td><td>Store word to memory</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">; Direct register addressing
stb rg0, rg1 ; Store byte from rg0 to address in rg1
stw rg0, rg1, 12 ; Store word to (rg1 + 12)
; Label addressing
stb acc, buffer ; Store byte from accumulator to 'buffer'
stw rg1, current ; Store word to 'current' variable
</code></pre>
<p><strong>Label Expansions:</strong></p>
<pre><code class="language-dsa">; stb acc, buffer expands to:
lli buffer, rgf ; Load lower 16 bits of buffer address
lui buffer, rgf ; Load upper 16 bits of buffer address
stb acc, rgf ; Store byte from acc to address in rgf
; stw rg1, current expands to:
lli current, rgf ; Load lower 16 bits of current address
lui current, rgf ; Load upper 16 bits of current address
stw rg1, rgf ; Store word from rg1 to address in rgf
</code></pre>
<h3 id="immediate-load-instructions"><a class="header" href="#immediate-load-instructions">Immediate Load Instructions</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>LLI</strong></td><td><code>imm, dest_reg</code></td><td>Load 16-bit immediate into lower 16 bits<br/><strong>Clears upper 16 bits!</strong></td></tr>
<tr><td><strong>LUI</strong></td><td><code>imm, dest_reg</code></td><td>Load 16-bit immediate into upper 16 bits</td></tr>
</tbody></table>
</div>
<p><strong>Usage</strong></p>
<p>ensure that you always run <strong>Lli</strong> before <strong>Lui</strong> as <strong>Lli</strong> clears the upper 16 bits.</p>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">lli 0x1234, rg0 ; Load 0x1234 into lower 16 bits of rg0
lui 0xABCD, rg0 ; Load 0xABCD into upper 16 bits of rg0
</code></pre>
<h3 id="jump-instructions"><a class="header" href="#jump-instructions">Jump Instructions</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>JMP</strong></td><td><code>addr [, offset_reg]</code><br><code>imm, offset_reg</code></td><td>Unconditional jump</td></tr>
<tr><td><strong>JEQ</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if equal flag set</td></tr>
<tr><td><strong>JNE</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if not equal flag set</td></tr>
<tr><td><strong>JGT</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if greater than flag set</td></tr>
<tr><td><strong>JGE</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if greater or equal flags set</td></tr>
<tr><td><strong>JLT</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if less than flag set</td></tr>
<tr><td><strong>JLE</strong></td><td><code>addr [, offset_reg]</code></td><td>Jump if less or equal flags set</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">jmp start ; Jump to label 'start'
jmp 4, ret ; Jump to address (4 + ret register)
jeq end ; Jump to 'end' if equal flag set
jgt loop ; Jump to 'loop' if greater than flag set
</code></pre>
<h3 id="arithmetic-instructions"><a class="header" href="#arithmetic-instructions">Arithmetic Instructions</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>ADD</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Addition</td></tr>
<tr><td><strong>SUB</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Subtraction</td></tr>
<tr><td><strong>IADD</strong></td><td><code>src_reg, imm [, dest_reg]</code></td><td>Immediate addition</td></tr>
<tr><td><strong>ISUB</strong></td><td><code>src_reg, imm [, dest_reg]</code></td><td>Immediate subtraction</td></tr>
<tr><td><strong>INC</strong></td><td><code>reg</code></td><td>Increment register by 1</td></tr>
<tr><td><strong>DEC</strong></td><td><code>reg</code></td><td>Decrement register by 1</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">add rg0, rg1, rg2 ; rg2 = rg0 + rg1
sub rg0, rg1, rg2 ; rg2 = rg0 - rg1
iadd rg0, 10 ; rg0 = rg0 + 10
// or using alternate syntax
addi rg0, 1 ; rg0 = rg0 + 1
inc rg0 ; rg0 = rg0 + 1
</code></pre>
<h3 id="bitwise-operations"><a class="header" href="#bitwise-operations">Bitwise Operations</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>AND</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise AND</td></tr>
<tr><td><strong>OR</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise OR</td></tr>
<tr><td><strong>XOR</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise XOR</td></tr>
<tr><td><strong>NOT</strong></td><td><code>src_reg, dest_reg</code></td><td>Bitwise NOT</td></tr>
<tr><td><strong>NAND</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise NAND</td></tr>
<tr><td><strong>NOR</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise NOR</td></tr>
<tr><td><strong>XNOR</strong></td><td><code>src1_reg, src2_reg, dest_reg</code></td><td>Bitwise XNOR</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">and rg0, rg1, rg2 ; rg2 = rg0 &amp; rg1
not rg0, rg1 ; rg1 = ~rg0
</code></pre>
<h3 id="shift-operations"><a class="header" href="#shift-operations">Shift Operations</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>SHL</strong></td><td><code>reg, shift_amount</code></td><td>Shift left</td></tr>
<tr><td><strong>SHR</strong></td><td><code>reg, shift_amount</code></td><td>Shift right</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">shl rg0, 2 ; Shift rg0 left by 2 bits
shr rg0, 3 ; Shift rg0 right by 3 bits
</code></pre>
<h3 id="comparison-and-control"><a class="header" href="#comparison-and-control">Comparison and Control</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>CMP</strong></td><td><code>reg1, reg2</code></td><td>Compare registers and set flags</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">cmp rg0, zero ; Compare rg0 with zero register
cmp rg1, rg2 ; Compare rg1 with rg2
</code></pre>
<h3 id="system-instructions"><a class="header" href="#system-instructions">System Instructions</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Mnemonic</th><th>Operands</th><th>Description</th></tr></thead><tbody>
<tr><td><strong>HLT</strong></td><td>-</td><td>Halt processor execution</td></tr>
<tr><td><strong>NOP</strong></td><td>-</td><td>No operation</td></tr>
<tr><td><strong>INT</strong></td><td><code>interrupt_code</code></td><td>Trigger interrupt</td></tr>
<tr><td><strong>IRT</strong></td><td>-</td><td>Return from interrupt</td></tr>
</tbody></table>
</div>
<p><strong>Examples:</strong></p>
<pre><code class="language-dsa">hlt ; Stop processor execution
int 0x21 ; Trigger interrupt 0x21
</code></pre>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../dsa/instructions.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../dsa/instructions/pseudo.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../dsa/instructions.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../dsa/instructions/pseudo.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>