TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Subroutines in MIX are called using self-modifying code

20 点作者 deanmen将近 11 年前

6 条评论

falava将近 11 年前
Quoting Donald Knuth from MMIX 2009 [1] (the revision of the MIX architecture):<p>&quot;And ouch, the standard subroutine calling convention of MIX is irrevocably based on self-modifying instructions! Decimal arithmetic and self-modifying code were popular in 1962, but they sure have disappeared quickly as machines have gotten bigger and faster.&quot;<p>[1] <a href="http://www-cs-faculty.stanford.edu/~uno/mmix.html" rel="nofollow">http:&#x2F;&#x2F;www-cs-faculty.stanford.edu&#x2F;~uno&#x2F;mmix.html</a>
kps将近 11 年前
Many early machines did this, often using a special form of store instruction that only modified the target address part of an instruction word.<p>EDSAC, on which David Wheeler invented the subroutine call, had no such instruction, and did it like this (using a modern pseudo-assembler syntax, not the original):<p><pre><code> address instruction comment ------- ----------------- --------- m - 1. clear accumulator m. add from m # Load the &#x27;add from m&#x27; instruction itself # (which contains its own address) into # the accumulator. m + 1. jump to s # Jump to the subroutine. m + 2. … # Control will return here. ⋮ s. add from 3 # Add a magic constant (kept in location 3) # to the accumulator (which contains the # &#x27;add from m&#x27; instruction from m) resulting # in a &#x27;jump to m+2&#x27; instruction. s+1. store to r # Store the &#x27;jump to m+2&#x27; instruction at the # end of the subroutine. ⋮ # Subroutine body. r. (anything) # ‘Return’ jump stored here.</code></pre>
abecedarius将近 11 年前
Ken Thompson&#x27;s regular-expression matcher also worked this way. It used two arrays of jump instructions: one jump for each current NFA state, jumping into the machine code that had been compiled for the regex. These jumps were actually calls using self-modifying code as in the OP (but for IBM 709 instead of MIX). But the new wrinkle was, it also wrote more jump instructions into the <i>other</i> array, for the next-state set of the NFA matcher.<p>Unlike the MIX, the IBM 709 had a jump-through-register instruction, so self-modifying code wasn&#x27;t necessary. I found that Thompson&#x27;s code could&#x27;ve been more compact and about as fast (sometimes faster, sometimes slower) using such indirect jumps only, at the cost of needing to use all of the registers.
评论 #8078546 未加载
userbinator将近 11 年前
It&#x27;s worth noting that JIT compilation techniques are also an extremely advanced form of self-modifying code.
评论 #8078142 未加载
mmagin将近 11 年前
Ironically, I was just learning this morning how it&#x27;s done on the PDP-8, which has a disturbingly similar flavor. <a href="http://en.wikipedia.org/wiki/PDP-8#Subroutines" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;PDP-8#Subroutines</a>
pcwalton将近 11 年前
This approach doesn&#x27;t work with recursive functions, of course. The chapter notes this: &quot;Reentrant routines are useful in relatively rare circumstances.&quot;<p>Oh, how times have changed…