<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Writings</title>
	<subtitle>My writings</subtitle>
	<link rel="self" type="application/atom+xml" href="https://loga4m.uz/posts/feed.xml"/>
  <link rel="alternate" type="text/html" href="https://loga4m.uz/posts/"/>
  
	<updated>2026-03-09T00:00:00+00:00</updated>
	
	<id>https://loga4m.uz/posts/feed.xml</id>
	<entry xml:lang="en">
		<title>Fix on system freezes due to GPU problem on ASUS ROG Strix G513M</title>
		<published>2026-03-09T00:00:00+00:00</published>
		<updated>2026-03-09T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://loga4m.uz/posts/gpu-fix/"/>
		<id>https://loga4m.uz/posts/gpu-fix/</id>
    
		<content type="html" xml:base="https://loga4m.uz/posts/gpu-fix/">&lt;h1 id=&quot;asus-rog-strix-g513rm-linux-freeze-gpu-fix&quot;&gt;ASUS ROG Strix G513RM — Linux Freeze &amp;amp; GPU Fix&lt;a class=&quot;zola-anchor&quot; href=&quot;#asus-rog-strix-g513rm-linux-freeze-gpu-fix&quot; aria-label=&quot;Anchor link for: asus-rog-strix-g513rm-linux-freeze-gpu-fix&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;hardware&quot;&gt;Hardware&lt;a class=&quot;zola-anchor&quot; href=&quot;#hardware&quot; aria-label=&quot;Anchor link for: hardware&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Model:&lt;&#x2F;strong&gt; ASUS ROG Strix G513RM&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;CPU:&lt;&#x2F;strong&gt; AMD Ryzen (Rembrandt)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;GPUs:&lt;&#x2F;strong&gt; AMD Radeon 680M (iGPU) + NVIDIA RTX 3060 Mobile (dGPU)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;BIOS:&lt;&#x2F;strong&gt; G513RM.327 (latest available as of March 2026)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;a class=&quot;zola-anchor&quot; href=&quot;#problem&quot; aria-label=&quot;Anchor link for: problem&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;Persistent system freezes across multiple Linux distros. Root cause was the NVIDIA dGPU producing &lt;strong&gt;PCIe BadTLP errors&lt;&#x2F;strong&gt; (Bad Transaction Layer Packets) — corrupted communication between CPU and GPU over the PCIe bus, caused by PCIe ASPM power management dropping the link into an unstable state.&lt;&#x2F;p&gt;
&lt;p&gt;Secondary symptom: on warm reboot (not cold boot), the dGPU would disappear entirely from the PCIe bus (&lt;code&gt;lspci&lt;&#x2F;code&gt; returned nothing), requiring a full power cycle to recover.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;diagnosis&quot;&gt;Diagnosis&lt;a class=&quot;zola-anchor&quot; href=&quot;#diagnosis&quot; aria-label=&quot;Anchor link for: diagnosis&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;confirming-badtlp-errors&quot;&gt;Confirming BadTLP errors&lt;a class=&quot;zola-anchor&quot; href=&quot;#confirming-badtlp-errors&quot; aria-label=&quot;Anchor link for: confirming-badtlp-errors&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; dmesg&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; badtlp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Output showed repeated:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nvidia 0000:01:00.0: PCIe Bus Error: severity=Correctable, type=Data Link Layer, (Receiver ID)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nvidia 0000:01:00.0:  [ 6] BadTLP&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;confirming-gpu-presence&quot;&gt;Confirming GPU presence&lt;a class=&quot;zola-anchor&quot; href=&quot;#confirming-gpu-presence&quot; aria-label=&quot;Anchor link for: confirming-gpu-presence&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;lspci&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nvidia&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;#&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Expected: 01:00.0 VGA compatible controller: NVIDIA Corporation GA106M [GeForce RTX 3060 Mobile]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;fix&quot;&gt;Fix&lt;a class=&quot;zola-anchor&quot; href=&quot;#fix&quot; aria-label=&quot;Anchor link for: fix&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;step-1-enable-nvidia-dynamic-power-management&quot;&gt;Step 1: Enable NVIDIA Dynamic Power Management&lt;a class=&quot;zola-anchor&quot; href=&quot;#step-1-enable-nvidia-dynamic-power-management&quot; aria-label=&quot;Anchor link for: step-1-enable-nvidia-dynamic-power-management&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;Create &lt;code&gt;&#x2F;etc&#x2F;modprobe.d&#x2F;nvidia.conf&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nano&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;modprobe.d&#x2F;nvidia.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Add:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;options nvidia NVreg_DynamicPowerManagement=0x02&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This allows the driver to properly manage GPU power state, preventing the PCIe link from going unstable.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-2-use-acpi-reboot-method&quot;&gt;Step 2: Use ACPI reboot method&lt;a class=&quot;zola-anchor&quot; href=&quot;#step-2-use-acpi-reboot-method&quot; aria-label=&quot;Anchor link for: step-2-use-acpi-reboot-method&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nano&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;default&#x2F;grub&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Change:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GRUB_CMDLINE_LINUX_DEFAULT=&amp;quot;quiet splash reboot=acpi&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; update-grub&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This forces the kernel to use ACPI reboot instead of the default method, which properly resets PCIe device states on warm reboot.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-3-full-power-cycle-once&quot;&gt;Step 3: Full power cycle (once)&lt;a class=&quot;zola-anchor&quot; href=&quot;#step-3-full-power-cycle-once&quot; aria-label=&quot;Anchor link for: step-3-full-power-cycle-once&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;After applying the above, do a full &lt;strong&gt;poweroff&lt;&#x2F;strong&gt; (not reboot), wait 10 seconds, then power on. This resets the EC (Embedded Controller) state.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; poweroff&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;verification&quot;&gt;Verification&lt;a class=&quot;zola-anchor&quot; href=&quot;#verification&quot; aria-label=&quot;Anchor link for: verification&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;After cold boot:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;#&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; No output = no errors&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; dmesg&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; badtlp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;#&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Should show GPU stats&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;nvidia-smi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment&quot;&gt;#&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; Should show GPU on bus&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;lspci&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; nvidia&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;a class=&quot;zola-anchor&quot; href=&quot;#notes&quot; aria-label=&quot;Anchor link for: notes&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;why-warm-reboot-loses-the-gpu&quot;&gt;Why warm reboot loses the GPU&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-warm-reboot-loses-the-gpu&quot; aria-label=&quot;Anchor link for: why-warm-reboot-loses-the-gpu&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;The ASUS ROG G513RM firmware (EC) does not properly restore dGPU power state on warm reboot — only on cold boot. The &lt;code&gt;reboot=acpi&lt;&#x2F;code&gt; parameter mitigates this in most cases. If the GPU still disappears after a warm reboot, a full power cycle (poweroff → wait → power on) always recovers it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;kvm-conflicts-with-virtualbox&quot;&gt;KVM conflicts with VirtualBox&lt;a class=&quot;zola-anchor&quot; href=&quot;#kvm-conflicts-with-virtualbox&quot; aria-label=&quot;Anchor link for: kvm-conflicts-with-virtualbox&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;KVM (&lt;code&gt;kvm_amd&lt;&#x2F;code&gt;) is loaded at boot. VirtualBox&#x27;s &lt;code&gt;vboxdrv&lt;&#x2F;code&gt; kernel module grabs a reference to KVM at load time, making it impossible to unload KVM while VirtualBox is installed. If running 64-bit VMs in VirtualBox fails with an i686 error, blacklist KVM:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;blacklist kvm_amd&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; tee&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;modprobe.d&#x2F;blacklist-kvm.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;blacklist kvm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; tee&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F;etc&#x2F;modprobe.d&#x2F;blacklist-kvm.conf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; update-initramfs&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;u&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; reboot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;wayland-nvidia&quot;&gt;Wayland + NVIDIA&lt;a class=&quot;zola-anchor&quot; href=&quot;#wayland-nvidia&quot; aria-label=&quot;Anchor link for: wayland-nvidia&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;With the driver working correctly, GNOME Wayland session functions normally with the NVIDIA dGPU. Select &quot;Ubuntu on Wayland&quot; at the GDM login screen. Three-finger gestures and pinch-to-zoom work on Wayland; they do not work on X11.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;armoury-crate-control-service-update&quot;&gt;Armoury Crate Control Service update&lt;a class=&quot;zola-anchor&quot; href=&quot;#armoury-crate-control-service-update&quot; aria-label=&quot;Anchor link for: armoury-crate-control-service-update&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;During troubleshooting, an update for Armoury Crate Control Service was applied via MyASUS on Windows. This service manages EC firmware and hardware control on ROG laptops. It may have patched EC behavior related to dGPU power state on warm reboot, and could be partially responsible for warm reboot stability alongside &lt;code&gt;reboot=acpi&lt;&#x2F;code&gt;. If warm reboot GPU loss recurs on a fresh install, check for Armoury Crate updates in Windows before assuming the Linux fix is insufficient.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-does-not-work&quot;&gt;What does NOT work&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-does-not-work&quot; aria-label=&quot;Anchor link for: what-does-not-work&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;Global &lt;code&gt;pcie_aspm=off&lt;&#x2F;code&gt; or &lt;code&gt;pcie_port_pm=off&lt;&#x2F;code&gt; kernel parameters cause the GPU to disappear from the PCIe bus entirely and require a full power cycle to recover. Do not use these.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;em&gt;Document was generated by Claude.ai&#x2F;Sonnet 4.6.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Essentials of Computer Architecture and Assembly</title>
		<published>2026-03-01T00:00:00+00:00</published>
		<updated>2026-03-01T00:00:00+00:00</updated>
		<link rel="alternate" type="text/html" href="https://loga4m.uz/posts/cp-arch-asm/"/>
		<id>https://loga4m.uz/posts/cp-arch-asm/</id>
    
		<content type="html" xml:base="https://loga4m.uz/posts/cp-arch-asm/">
&lt;blockquote class=&quot;callout warning no-title&quot;&gt;
  
  
  &lt;div class=&quot;icon&quot;&gt;
    &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;18&quot; height=&quot;18&quot;&gt;&lt;path d=&quot;M12.865 3.00017L22.3912 19.5002C22.6674 19.9785 22.5035 20.5901 22.0252 20.8662C21.8732 20.954 21.7008 21.0002 21.5252 21.0002H2.47266C1.92037 21.0002 1.47266 20.5525 1.47266 20.0002C1.47266 19.8246 1.51886 19.6522 1.60663 19.5002L11.1329 3.00017C11.4091 2.52187 12.0206 2.358 12.4989 2.63414C12.651 2.72191 12.7772 2.84815 12.865 3.00017ZM4.20471 19.0002H19.7932L11.9989 5.50017L4.20471 19.0002ZM10.9989 16.0002H12.9989V18.0002H10.9989V16.0002ZM10.9989 9.00017H12.9989V14.0002H10.9989V9.00017Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;

  &lt;&#x2F;div&gt;
  &lt;div class=&quot;content&quot;&gt;
    &lt;p&gt;This article simplifies&#x2F;skips a lot of things. It is mainly intended to make
picking up assembly easier. Also, this is a write up of what I have
learned and understood over the course of reading CS:APP book and
doing some assembly exercises on &lt;strong&gt;Windows&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt; This article is work in progress. I am filling the missing sections in my free time.&lt;&#x2F;p&gt;

  &lt;&#x2F;div&gt;
  
&lt;&#x2F;blockquote&gt;

&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;a class=&quot;zola-anchor&quot; href=&quot;#introduction&quot; aria-label=&quot;Anchor link for: introduction&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;This guide is a mix of topics from computer architecture,
operating systems, and assembly programming.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;cpu&quot;&gt;CPU&lt;a class=&quot;zola-anchor&quot; href=&quot;#cpu&quot; aria-label=&quot;Anchor link for: cpu&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;The CPU, or &lt;em&gt;&lt;strong&gt;Central Processing Unit&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;, is a unit of computer hardware
that &lt;strong&gt;executes instructions&lt;&#x2F;strong&gt;. It is the &quot;brain&quot; of a computer system.&lt;&#x2F;p&gt;
&lt;p&gt;However,
it is not much clever as brain is. What it does is just to &lt;strong&gt;fetch&lt;&#x2F;strong&gt; next instruction, &lt;strong&gt;decode&lt;&#x2F;strong&gt; it,
and &lt;strong&gt;execute&lt;&#x2F;strong&gt; it. That&#x27;s it. This is called the &lt;em&gt;fetch-execute cycle&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In a simple model of single CPU which executes instructions &lt;em&gt;sequentially&lt;&#x2F;em&gt;, one at a time, the CPU
mainly consists of small memory, decoder, and Arithmetic Logic Unit (ALU).
It is also connected to an intermediary called &lt;strong&gt;System Bus&lt;&#x2F;strong&gt; which connects the CPU
to the other components in a computer hardware.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;strong&gt;ALU&lt;&#x2F;strong&gt; is the unit responsible for carrying operations like addition, subtraction,
multiplication, etc.&lt;&#x2F;p&gt;
&lt;p&gt;The memory of CPU, called &lt;strong&gt;register file&lt;&#x2F;strong&gt; (or just &lt;strong&gt;set&lt;&#x2F;strong&gt; of registers), is the
set of fastest memory elements in computer hardware which are used for storing operation results, intermediate results, addresses,
or any value that fits their capacity.&lt;&#x2F;p&gt;
&lt;p&gt;Analogously, if we say a CPU is a person solving a math problem, then register file
(the set of registers) is a collection of scratch papers.&lt;&#x2F;p&gt;
&lt;p&gt;Below is a simplified diagram.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;simple_cpu.png&quot; alt=&quot;Simplified CPU diagram&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;registers&quot;&gt;Registers&lt;a class=&quot;zola-anchor&quot; href=&quot;#registers&quot; aria-label=&quot;Anchor link for: registers&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;We can talk a lot about registers.&lt;&#x2F;p&gt;
&lt;p&gt;So, what is a register? A register is a storage with a fixed capacity (in bits). It just stores
plain bits.&lt;&#x2F;p&gt;
&lt;p&gt;There are mainly two types of CPU registers: &lt;em&gt;General Purpose&lt;&#x2F;em&gt; and &lt;em&gt;Special Registers&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;General Purpose Registers&lt;&#x2F;em&gt; (GPRs) are those we are given access to, while &lt;em&gt;special registers&lt;&#x2F;em&gt;
are mostly limited to the usage by the CPU.&lt;&#x2F;p&gt;
&lt;p&gt;One important register is called &lt;strong&gt;Program Counter&lt;&#x2F;strong&gt; (or instruction pointer). It stores
the &lt;em&gt;address&lt;&#x2F;em&gt; of the next instruction.&lt;&#x2F;p&gt;
&lt;p&gt;In x86-64 systems, registers typically have capacity of 64 bits. Due to historical reasons, &lt;strong&gt;in terms
of access&lt;&#x2F;strong&gt;, they are &quot;divided&quot; into parts: lower 64-bit, lower 32-bit and lower 16-bit.&lt;&#x2F;p&gt;
&lt;p&gt;The lower 16-bit itself has upper and lower bytes. This means we can use the upper byte
or lower byte -- directly. In contrast, some GPRs &lt;strong&gt;do not have&lt;&#x2F;strong&gt; this support of directly
using the &lt;strong&gt;upper byte&lt;&#x2F;strong&gt; of lower 16-bit.&lt;&#x2F;p&gt;
&lt;p&gt;This division gives us the ability to access&#x2F;use the specified portions of registers.
In other words, we can directly access parts contained in the boxes (yes, boxes).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;register.png&quot; alt=&quot;Simplified register parts&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Why is this due to historical reasons? As registers had lower capacity than 64-bit in the &lt;em&gt;past&lt;&#x2F;em&gt;,
hardware manufacturers modernized the processors with &lt;strong&gt;backward-compatibility&lt;&#x2F;strong&gt; in mind
so that old programs could run on &lt;em&gt;newer processors&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Note, this &quot;division&quot; is crucial -- since we quite often use them in assembly.&lt;&#x2F;p&gt;
&lt;p&gt;Below is a diagram presenting some of registers in x64 (&lt;a rel=&quot;nofollow noreferrer external&quot; href=&quot;https:&#x2F;&#x2F;cs.brown.edu&#x2F;courses&#x2F;cs033&#x2F;docs&#x2F;guides&#x2F;x64_cheatsheet.pdf&quot;&gt;source&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;registers.png&quot; alt=&quot;Register organization in x64&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;instruction-set-architecture&quot;&gt;Instruction Set Architecture&lt;a class=&quot;zola-anchor&quot; href=&quot;#instruction-set-architecture&quot; aria-label=&quot;Anchor link for: instruction-set-architecture&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;The next important element is &lt;strong&gt;ISA&lt;&#x2F;strong&gt;, or &lt;em&gt;Instruction Set Architecture&lt;&#x2F;em&gt;. For CPUs to be useful,
they need to execute instructions. I define an instruction as a set of rules that specify
&lt;strong&gt;which operations&lt;&#x2F;strong&gt; to carry on and &lt;strong&gt;on what operands&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The ISA provides the set of such instructions the CPU can &lt;strong&gt;understand&lt;&#x2F;strong&gt; and &lt;strong&gt;execute&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This provides us with a powerful abstraction over CPU. Also, it enables us to view
the execution process as sequential, executing one instruction at a time. In reality, the modern CPUs
are quite sophisticated and even support executing multiple instructions simultaneously.&lt;&#x2F;p&gt;
&lt;p&gt;In conclusion, ISA is the asset for us to talk to the CPU.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;main-memory&quot;&gt;Main Memory&lt;a class=&quot;zola-anchor&quot; href=&quot;#main-memory&quot; aria-label=&quot;Anchor link for: main-memory&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;One may note that registers are quite small and not enough for large sizes
of data our computers process on daily basis.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, we need a larger memory, and it is called the &lt;em&gt;main memory&lt;&#x2F;em&gt;. However, compared to registers,
the access time to main memory is quite slow. This is a trade off.&lt;&#x2F;p&gt;
&lt;p&gt;For now, we assume that the main memory is a very large &lt;em&gt;array of bytes&lt;&#x2F;em&gt; we can &lt;strong&gt;address&lt;&#x2F;strong&gt; and &lt;strong&gt;access&lt;&#x2F;strong&gt;.
In fact, this is how our &lt;em&gt;programs&lt;&#x2F;em&gt; view the memory (moer about it later).&lt;&#x2F;p&gt;
&lt;p&gt;Keep in mind this assumption as it is &lt;em&gt;very important&lt;&#x2F;em&gt; in assembly.&lt;&#x2F;p&gt;
&lt;p&gt;Below is a diagram depicting the memory as Post Office boxes from the book &lt;em&gt;&lt;strong&gt;Programming From Ground up&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;.
As you can note, each box has an address (or, an identifier&#x2F;label) associated with it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;memory_as_po.png&quot; alt=&quot;Memory depicted as Post Office Box&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;One question arises: how do we address memory and how much can we have of memory? The answer is what the &quot;X&quot;
means when we say &quot;X-bit architecture&quot;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-x-means-in-x-bit-architecture&quot;&gt;What &quot;X&quot; means in &quot;X-bit architecture&quot;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-x-means-in-x-bit-architecture&quot; aria-label=&quot;Anchor link for: what-x-means-in-x-bit-architecture&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;Looking at the organization of registers, one may note that X-bit system mainly has registers with capacity of X bits.
In other words, X-bit system can handle X-bit sized data at once.&lt;&#x2F;p&gt;
&lt;p&gt;Another meaning which follows from the size of registers is &lt;strong&gt;how many addresses&lt;&#x2F;strong&gt; we can have &lt;strong&gt;for&lt;&#x2F;strong&gt; our
&lt;strong&gt;memory locations&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using basic counting from math, we can derive that 64-bit system can address 2^64 &lt;em&gt;&lt;strong&gt;bytes&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; of memory. Note, this is not &lt;em&gt;bits&lt;&#x2F;em&gt;, but &lt;strong&gt;bytes&lt;&#x2F;strong&gt;.
This is because each memory location is &lt;strong&gt;one byte&lt;&#x2F;strong&gt; in &lt;em&gt;capacity&lt;&#x2F;em&gt;. In the analogy of Post Office Boxes,
we can have 2^64 boxes with &lt;strong&gt;unique addresses&lt;&#x2F;strong&gt; and each having 1 byte of capacity.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;re confused about addresses and calculation, think of addresses as &quot;labels&quot; to locations. So, we can label 2^X memory locations,
reuslting in memory as large as 2^X * capacity_of_single_location.&lt;&#x2F;p&gt;
&lt;p&gt;Subsequently, with 32 bits we can have memory as large as 2^32 * 1 byte ~= 4GB while with 64 bits theoretically we can have
16 exabytes!&lt;&#x2F;p&gt;

&lt;blockquote class=&quot;callout note has-title&quot;&gt;
  
  
  &lt;p class=&quot;title&quot;&gt;
    &lt;span class=&quot;icon&quot;&gt;
      &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;18&quot; height=&quot;18&quot;&gt;&lt;path d=&quot;M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 7H13V9H11V7ZM11 11H13V17H11V11Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;

    &lt;&#x2F;span&gt;
    &lt;strong&gt;Main memory vs. Secondary memory&lt;&#x2F;strong&gt;
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Main memory&lt;&#x2F;strong&gt;: smaller, faster, and volatile (data is lost when a system goes off).
&lt;strong&gt;Secondary memory&lt;&#x2F;strong&gt;: larger, slower, and non-volatile (data persists).&lt;&#x2F;p&gt;
&lt;p&gt;As you may have noticed, the smaller is the memory, the faster it is; the larger is the memory, the slower it is (in terms of access time).
This is part of the &lt;em&gt;&lt;strong&gt;memory hierarchy&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The main memory can be in form of RAM, while secondary memory can be HDD, SSD, etc.&lt;&#x2F;p&gt;

  &lt;&#x2F;div&gt;
  
&lt;&#x2F;blockquote&gt;

&lt;p&gt;Our model so far...
&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;cpu_with_main_mem.png&quot; alt=&quot;Our CPU and memory model&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;intro-to-assembly&quot;&gt;Intro to Assembly&lt;a class=&quot;zola-anchor&quot; href=&quot;#intro-to-assembly&quot; aria-label=&quot;Anchor link for: intro-to-assembly&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;Remember that the CPU&#x27;s main task is to fetch, decode, and execute instructions.
Reminding again, the ISA of each CPU provides instructions &lt;strong&gt;valid&lt;&#x2F;strong&gt; for that CPU.&lt;&#x2F;p&gt;
&lt;p&gt;Now, how do we write those instructions for CPUs?
As you may already know, CPUs only understand 0s and 1s, which is &lt;strong&gt;machine-language&lt;&#x2F;strong&gt;. Nothing more. Just bit patterns + &lt;strong&gt;context&lt;&#x2F;strong&gt; (with context, for example, numbers may
denote letters).&lt;&#x2F;p&gt;
&lt;p&gt;However, as humans, it could be burdensome to talk to CPUs in plain bits.
Here, the Assembly language gives us one higher level of abstraction and makes writing instructions much more
readable, essentially being a wrapper over the machine language.&lt;&#x2F;p&gt;
&lt;p&gt;As our instructions are based on the ISA of our machine, assembly written
for different ISAs is not the same, and incompatible.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, assembly is &lt;em&gt;&lt;strong&gt;machine-dependent&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt;. In this sense, we can call assembly a &lt;em&gt;&lt;strong&gt;machine language&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; to distinguish
from higher level languages such as C which are much more machine &lt;strong&gt;independent&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;There are many variations of assembly language, differing in syntax and some features. However, the
idea is the same: just write the instructions for the CPU.&lt;&#x2F;p&gt;
&lt;p&gt;I am going to use NASM syntax.&lt;&#x2F;p&gt;
&lt;p&gt;In the following sections, I write about the most essential assembly concepts, but without
giving full &lt;em&gt;program code&lt;&#x2F;em&gt;. Just bare plain concepts.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;movement-operations&quot;&gt;Movement operations&lt;a class=&quot;zola-anchor&quot; href=&quot;#movement-operations&quot; aria-label=&quot;Anchor link for: movement-operations&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;The main operands of instructions is data. One of the most common operations we do is
data movement&#x2F;copy&#x2F;write&#x2F;read.&lt;&#x2F;p&gt;
&lt;p&gt;The instruction that copies data from one &lt;strong&gt;source&lt;&#x2F;strong&gt; to &lt;strong&gt;destination&lt;&#x2F;strong&gt; in assembly
is called &lt;code&gt;mov&lt;&#x2F;code&gt; with the following form:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mov DATASIZE dest, src&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The operation copies bytes of amount specified using &lt;code&gt;DATASIZE&lt;&#x2F;code&gt; from source &lt;code&gt;src&lt;&#x2F;code&gt; to &lt;code&gt;dest&lt;&#x2F;code&gt;.
Although this definition covers all operand forms, we will see that there is subtlety in
how the process looks like depending on operand forms (the operands are &lt;code&gt;dest&lt;&#x2F;code&gt; and &lt;code&gt;src&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Since we to specify data sizes and that they are very important in other instructions too,
the following section presents some of the data sizes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;common-data-sizes&quot;&gt;Common data sizes&lt;a class=&quot;zola-anchor&quot; href=&quot;#common-data-sizes&quot; aria-label=&quot;Anchor link for: common-data-sizes&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Name&lt;&#x2F;th&gt;&lt;th&gt;Size&lt;&#x2F;th&gt;&lt;th&gt;Size (bits)&lt;&#x2F;th&gt;&lt;th&gt;NASM&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;byte&lt;&#x2F;td&gt;&lt;td&gt;1 byte&lt;&#x2F;td&gt;&lt;td&gt;8 bits&lt;&#x2F;td&gt;&lt;td&gt;op byte&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;word&lt;&#x2F;td&gt;&lt;td&gt;2 bytes&lt;&#x2F;td&gt;&lt;td&gt;16 bits&lt;&#x2F;td&gt;&lt;td&gt;op word&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;dword&lt;&#x2F;td&gt;&lt;td&gt;2 words&lt;&#x2F;td&gt;&lt;td&gt;32 bits&lt;&#x2F;td&gt;&lt;td&gt;op dword&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;qword (quad word)&lt;&#x2F;td&gt;&lt;td&gt;4 words&lt;&#x2F;td&gt;&lt;td&gt;64 bits&lt;&#x2F;td&gt;&lt;td&gt;op qword&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mov dword dest, src&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This instructions reads 4 bytes of data from &lt;code&gt;src&lt;&#x2F;code&gt; and write 4 bytes to &lt;code&gt;dest&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;operand-and-instruction-forms&quot;&gt;Operand and instruction forms&lt;a class=&quot;zola-anchor&quot; href=&quot;#operand-and-instruction-forms&quot; aria-label=&quot;Anchor link for: operand-and-instruction-forms&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In assembly, there are mainly three types of operands:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;register&lt;&#x2F;li&gt;
&lt;li&gt;immediate&lt;&#x2F;li&gt;
&lt;li&gt;address in the &lt;em&gt;program&lt;&#x2F;em&gt; memory&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The instructions we write are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;binary -- takes 2 operands&lt;&#x2F;li&gt;
&lt;li&gt;unary -- takes one operand&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The &lt;code&gt;mov&lt;&#x2F;code&gt; instruction is binary. Thus, its operands are the mix of three, with some combinations disallowed.&lt;&#x2F;p&gt;
&lt;p&gt;The following is the table of usage patterns.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Type&lt;&#x2F;th&gt;&lt;th&gt;Form&lt;&#x2F;th&gt;&lt;th&gt;Value in operation&lt;&#x2F;th&gt;&lt;th&gt;Meaning&lt;&#x2F;th&gt;&lt;th&gt;Explanation&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Immediate&lt;&#x2F;td&gt;&lt;td&gt;Imm&lt;&#x2F;td&gt;&lt;td&gt;Imm&lt;&#x2F;td&gt;&lt;td&gt;literal value&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Register&lt;&#x2F;td&gt;&lt;td&gt;r&lt;&#x2F;td&gt;&lt;td&gt;R[r]&lt;&#x2F;td&gt;&lt;td&gt;Value stored in register &lt;code&gt;r&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Mem&lt;&#x2F;td&gt;&lt;td&gt;[Imm]&lt;&#x2F;td&gt;&lt;td&gt;M[Imm]&lt;&#x2F;td&gt;&lt;td&gt;Value stored at memory address &lt;code&gt;Imm&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Mem&lt;&#x2F;td&gt;&lt;td&gt;[r]&lt;&#x2F;td&gt;&lt;td&gt;M[R[r]]&lt;&#x2F;td&gt;&lt;td&gt;Value stored at memory address stored in &lt;code&gt;r&lt;&#x2F;code&gt;.&lt;&#x2F;td&gt;&lt;td&gt;The register &lt;code&gt;r&lt;&#x2F;code&gt; stores a memory address as its value.&lt;br&gt;&lt;br&gt;Writing [r] means that &lt;code&gt;r&lt;&#x2F;code&gt; stores memory address and instructs CPU to work on data stored in that address.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Mem&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;[r1 + r2*k + Imm]&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;M[R[ r1 ] + R[ r2 ] * k + Imm]&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;This is arithmetic involving addresses.&lt;&#x2F;td&gt;&lt;td&gt;This form presents generic form for calculating address and accessing value starting at it in memory. For example, setting &lt;code&gt;k&lt;&#x2F;code&gt; and &lt;code&gt;Imm&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt;, we obtain the (2) form in the table.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;The following are diagrams explaining behavior of &lt;code&gt;mov&lt;&#x2F;code&gt; operation involving different operand forms.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;immediate-to-register&quot;&gt;Immediate to register&lt;a class=&quot;zola-anchor&quot; href=&quot;#immediate-to-register&quot; aria-label=&quot;Anchor link for: immediate-to-register&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;mov_examples&#x2F;immediate.png&quot; alt=&quot;Copying immediate value to register&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;register-to-register&quot;&gt;Register to register&lt;a class=&quot;zola-anchor&quot; href=&quot;#register-to-register&quot; aria-label=&quot;Anchor link for: register-to-register&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Description:&lt;&#x2F;strong&gt; copies value from &lt;code&gt;src&lt;&#x2F;code&gt; register to &lt;code&gt;dst&lt;&#x2F;code&gt; register (note the size specifier).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;mov_examples&#x2F;reg2reg.png&quot; alt=&quot;Copying from register to register&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt; the red underline is not accidental. This is one of  the special cases which is discussed later.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;memory-to-register&quot;&gt;Memory to register&lt;a class=&quot;zola-anchor&quot; href=&quot;#memory-to-register&quot; aria-label=&quot;Anchor link for: memory-to-register&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;Suppose we have the number 1324 in the memory locations shown in the image below.
Such a signed integer is typically 4 bytes.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;1324 in binary: 10100101100&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;To convert to hex, group the bits of the binary value by 4 from right to left and replace each group with hex equivalent.
Fill in zeros from left when number of bits in a group is not 4.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0101 0010 1100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 0x5  0x2  0xC =&amp;gt; 1324 in hex: 0x52c&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Joining them by 1 byte groups:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000101 00101100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   0x5      0x2c&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt; &#x27;0x&#x27; just denotes that the value is in hex.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;loga4m.uz&#x2F;posts&#x2F;cp-arch-asm&#x2F;.&#x2F;mov_examples&#x2F;mem2reg.png&quot; alt=&quot;Copying from memory to register&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt; You might have noticed little red asterisk. The sentence it is marking describes the behavior on one subtle case of &lt;code&gt;mov&lt;&#x2F;code&gt; operation. This will be discussed later.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;immediate-to-memory&quot;&gt;Immediate to memory&lt;a class=&quot;zola-anchor&quot; href=&quot;#immediate-to-memory&quot; aria-label=&quot;Anchor link for: immediate-to-memory&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;h3 id=&quot;register-to-memory&quot;&gt;Register to memory&lt;a class=&quot;zola-anchor&quot; href=&quot;#register-to-memory&quot; aria-label=&quot;Anchor link for: register-to-memory&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;h2 id=&quot;special-cases&quot;&gt;Special cases&lt;a class=&quot;zola-anchor&quot; href=&quot;#special-cases&quot; aria-label=&quot;Anchor link for: special-cases&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h1 id=&quot;cmp-test-operations&quot;&gt;CMP&#x2F;TEST operations&lt;a class=&quot;zola-anchor&quot; href=&quot;#cmp-test-operations&quot; aria-label=&quot;Anchor link for: cmp-test-operations&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conditions-flags&quot;&gt;Conditions&#x2F;FLAGS&lt;a class=&quot;zola-anchor&quot; href=&quot;#conditions-flags&quot; aria-label=&quot;Anchor link for: conditions-flags&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conditional-branching-intro-to-jumps&quot;&gt;Conditional branching: Intro to Jumps&lt;a class=&quot;zola-anchor&quot; href=&quot;#conditional-branching-intro-to-jumps&quot; aria-label=&quot;Anchor link for: conditional-branching-intro-to-jumps&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;from-source-to-binary&quot;&gt;From Source to Binary&lt;a class=&quot;zola-anchor&quot; href=&quot;#from-source-to-binary&quot; aria-label=&quot;Anchor link for: from-source-to-binary&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;object-file-structure&quot;&gt;Object File Structure&lt;a class=&quot;zola-anchor&quot; href=&quot;#object-file-structure&quot; aria-label=&quot;Anchor link for: object-file-structure&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;back-to-jumps&quot;&gt;Back to Jumps&lt;a class=&quot;zola-anchor&quot; href=&quot;#back-to-jumps&quot; aria-label=&quot;Anchor link for: back-to-jumps&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h1 id=&quot;conditional-movement-operations&quot;&gt;Conditional movement operations&lt;a class=&quot;zola-anchor&quot; href=&quot;#conditional-movement-operations&quot; aria-label=&quot;Anchor link for: conditional-movement-operations&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;into-to-stack&quot;&gt;Into to Stack&lt;a class=&quot;zola-anchor&quot; href=&quot;#into-to-stack&quot; aria-label=&quot;Anchor link for: into-to-stack&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hardware-manager-operating-system&quot;&gt;Hardware Manager: Operating System&lt;a class=&quot;zola-anchor&quot; href=&quot;#hardware-manager-operating-system&quot; aria-label=&quot;Anchor link for: hardware-manager-operating-system&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;abstracting-hardware-process&quot;&gt;Abstracting hardware: Process&lt;a class=&quot;zola-anchor&quot; href=&quot;#abstracting-hardware-process&quot; aria-label=&quot;Anchor link for: abstracting-hardware-process&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;abstracting-memory-virtual-address-space&quot;&gt;Abstracting Memory: Virtual Address Space&lt;a class=&quot;zola-anchor&quot; href=&quot;#abstracting-memory-virtual-address-space&quot; aria-label=&quot;Anchor link for: abstracting-memory-virtual-address-space&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;back-to-stack&quot;&gt;Back to Stack&lt;a class=&quot;zola-anchor&quot; href=&quot;#back-to-stack&quot; aria-label=&quot;Anchor link for: back-to-stack&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;h1 id=&quot;some-interesting-things&quot;&gt;Some interesting things&lt;a class=&quot;zola-anchor&quot; href=&quot;#some-interesting-things&quot; aria-label=&quot;Anchor link for: some-interesting-things&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;resources&quot;&gt;Resources&lt;a class=&quot;zola-anchor&quot; href=&quot;#resources&quot; aria-label=&quot;Anchor link for: resources&quot; style=&quot;visibility: hidden;&quot;&gt;&lt;&#x2F;a&gt;
&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;em&gt;Coming soon.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
</feed>
