TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C Portability Lessons from Weird Machines

154 pointsby rsecoraover 3 years ago

22 comments

zokierover 3 years ago
I don&#x27;t think there is anything wrong in writing platform-specific code; in certain circles there is this weird fetishitization of portability, placing it on the highest pedestal as a metric of quality. This happens in C programming and also for example in shell scripting, people advocating for relying only on POSIX-defined behavior. If a platform specific way of doing something works better in some use-case then there should be no shame in using that. What is important is that the code relies on well-defined behavior, and also that the platform assumptions&#x2F;requirements are documented to a degree.<p>Of course it is wonderful that you can make programs that are indeed portable between this huge range of computers; just that not every program needs to do so.
评论 #30422950 未加载
评论 #30420380 未加载
评论 #30421101 未加载
评论 #30420880 未加载
评论 #30419896 未加载
评论 #30421613 未加载
评论 #30420291 未加载
评论 #30423597 未加载
评论 #30422308 未加载
评论 #30425608 未加载
eqvinoxover 3 years ago
On a slightly related note, chances are good anyone reading this has an 8051 within a few meters of them - they&#x27;re close to omnipresent in USB chips, particularly hubs, storage bridges and keyboards &#x2F; mice. The architecture is equally atrocious as the 6502.<p>btw: a good indicator is GCC support - AVR, also an 8-bit µC - is perfectly well supported by GCC. 8051 and 6502, you need something like SDCC [<a href="http:&#x2F;&#x2F;sdcc.sourceforge.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;sdcc.sourceforge.net&#x2F;</a>]
评论 #30419442 未加载
评论 #30419986 未加载
评论 #30419932 未加载
评论 #30418966 未加载
评论 #30423522 未加载
评论 #30423735 未加载
评论 #30419695 未加载
WalterBrightover 3 years ago
I recall discussing C with a person using a processor where chars, shorts, and ints were all 32 bits. He stressed that writing portable code was necessary.<p>I pointed out that any programs that needed to manipulate byte data would require very special coding to work. Special enough to make it pointless to try to write portable code between it and a regular machine.<p>It&#x27;s unreasonable to contort the standard to support such machines. It is reasonable for the compiler author on such machines to make some adjustments to the language.<p>For example, one can say C++ is technically portable to 16 bit machines. But it is not in practice, because:<p>1. segmented 16 bit machines require near&#x2F;far pointer extensions<p>2. exceptions will not work on 16 bit machines, because supporting it consumes way too much memory<p>3. ditto for RTTI
rwmjover 3 years ago
C23 just dropped support for any non-twos-complement architectures. No more C on Unisys for you! <a href="http:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n2412.pdf" rel="nofollow">http:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n2412.pdf</a>
评论 #30420009 未加载
评论 #30422589 未加载
评论 #30423545 未加载
评论 #30420975 未加载
astrobe_over 3 years ago
&gt; Everyone who writes about programming the Intel 286 says what a pain its segmented memory architecture was<p>Actually this concerns more pre-80286 processors, since 80286 introduced virtual memory, and the segment registers were less prominent in &quot;protected mode&quot;. Moreover I wouldn&#x27;t say it was a pain, at least at the assembly level, once you understood the trick. C had not concept of segmented memory, so you had to tell the compiler which &quot;memory model&quot; it should use.<p>&gt; One significant quirk is that the machine is very sensitive to data alignment.<p>I remembered from school time about a &quot;barrel register&quot; that allowed to remove this limitation, but it was introduced in 68020.<p>On the topic itself, I like to say that a program is portable if it has been ported once (likewise a module is reusable if it has been reused once). I remember porting a program from a 68K descendant to ARM, the only non-obvious portability issue was that in C, the <i>char</i> type is that the standard doesn&#x27;t mandate the <i>char</i> type to be signed or unsigned (it&#x27;s implementation-defined).
评论 #30418701 未加载
评论 #30420510 未加载
评论 #30418702 未加载
评论 #30435313 未加载
zwiebackover 3 years ago
I wrote a fair amount of code for TI&#x27;s TMS320C4x DSPs. They had 32 bit sized char, short, int, long, float and double and a long double with 40 bits.<p>Took a bit to get used to but really the only way to get to the good stuff was by writing assembly code and hand-tuning all the pipeline stuff.
PhantomGremlinover 3 years ago
<i>the MIPS R3000 processor ... raises an exception for signed integer overflow, unlike many other processors which silently wrap to negative values.</i><p>Too bad programmer laziness won and most current hardware doesn&#x27;t support this.<p>As a teenager I remember getting hit by this all the time in assembly language programming for the IBM S&#x2F;360. (FORTRAN turned it off).<p><pre><code> S0C8 Fixed-point overflow exception </code></pre> When you&#x27;re a kid you just do things quickly. This was the machine&#x27;s way of slapping you upside your head and saying: &quot;are you sure about that?&quot;
评论 #30420014 未加载
评论 #30418654 未加载
评论 #30418565 未加载
评论 #30418590 未加载
评论 #30418455 未加载
rsecoraover 3 years ago
It still amazes me how the PDP-11 has the NUXI [1] problem at nibble level and how the PDP-11 was bytesexual [2].<p>[1] <a href="http:&#x2F;&#x2F;catb.org&#x2F;jargon&#x2F;html&#x2F;N&#x2F;NUXI-problem.html" rel="nofollow">http:&#x2F;&#x2F;catb.org&#x2F;jargon&#x2F;html&#x2F;N&#x2F;NUXI-problem.html</a><p>[2] <a href="http:&#x2F;&#x2F;catb.org&#x2F;jargon&#x2F;html&#x2F;B&#x2F;bytesexual.html" rel="nofollow">http:&#x2F;&#x2F;catb.org&#x2F;jargon&#x2F;html&#x2F;B&#x2F;bytesexual.html</a>
评论 #30418428 未加载
gwernover 3 years ago
Note: &quot;weird machines&quot; here has nothing to do with the well-known security concept, just referring to unusual or obscure computers.
anonymousiamover 3 years ago
Good article.<p>I&#x27;ve done C on half of those platforms. 8051 is actually more complex than described. There are two different kinds of RAM with two different addressing modes. IIRC there are 128 bytes of &quot;zero page&quot; RAM and, depending on the specific variant, somewhere between 256 bytes and a few kilobytes of &quot;normal&quot; RAM. Both RAM types can be present, and the addresses can both be the same value, but point to different memory, so the context of the RAM type is critical. The variants usually have a lot more ROM than RAM so coding styles may need to be adjusted, such as using a lot of (ROM initialized data) constants instead of computing things at run time.<p>6502 has a similar &quot;zero page&quot; addressing mode to the 8051.<p>I never encountered any alignment exceptions on 68k (Aztec C). Either I was oblivious and lucky, or just naturally wrote good code.<p>I do remember something about PDP-11 where there was a maximum code segment size (32k words?).<p>C on the VAX (where I first learned it) was a superset of the (not yet) ANSI standard. I vaguely remember some cases where the compiler&#x2F;environment would allow some lazy notation with regard to initialized data structures.<p>They left out some interesting platforms (such as 6809&#x2F;OS9, TI MSP430 and PPC), which have their own quirks.
nivertechover 3 years ago
The author forgot to mention that 8051 has a bit-addressable lower part of RAM.<p>PDP-11 had a weird RAM overlay scheme of squeezing 256KB RAM into a 64KB 16-bit address space.<p>IBM System&#x2F;360 also had a weird addressing scheme with base register and up to 4KB offsets.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;IBM_System&#x2F;360_architecture#Addressing" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;IBM_System&#x2F;360_architecture#Ad...</a>
ChuckMcMover 3 years ago
I scored 7. (have written C code on six of the architectures mentioned (PDP 11, i86, VAX, 68K, IBM 360, AT&amp;T 3B2, and DG Eclipse) I have also written C code on the DEC KL-10 (36 bit machine) which isn&#x27;t covered. And while I have a PDP-8, I only have FOCAL and FORTRAN for it rather than C. I&#x27;m sure there is a C compiler out there somewhere :-).<p>With the imminent C23 spec I&#x27;m really amazed at how well C has held up over the last half century. A lot of things in computers are very &#x27;temporal&#x27; (in that there are a lot of things that are all available at a certain point in time that are required for the system to work) but C has managed to dodge much of that.
kwertyoowiyopover 3 years ago
Given C’s origin on the PDP-11, it’s amazing it ended up so portable to all these crazy architectures. Even as an old-timer, the 8051 section made me say “WTF”!
jnwatsonover 3 years ago
I love these weird machines. I&#x27;ll give an example of another.<p>The Texas Instruments&#x27; C40 was a DSP made in the late 90&#x27;s. It had 32-bit words, but inefficient byte manipulation. The compiler&#x2F;ABI writer&#x27;s solution was simple: make char 32 bits. So sizeof(char)==sizeof(short)==sizeof(int)==sizeof(long).<p>I remember writing routines for &quot;packing&quot; and &quot;unpacking&quot; native strings to byte strings and back.
评论 #30423611 未加载
kazinatorover 3 years ago
Theoretical portability is not very useful. If you&#x27;ve not <i>tested</i> on a Unisys with 36 bit integers and 8 word function pointers, the theoretical port is garbage.<p>It may be easier to fix the code than if you didn&#x27;t think about the Unisys, but that effort has to be weighed against the incredibly vanishing odds of it ever being required.
ziml77over 3 years ago
While skimming did I miss an example of code that is portable between most of these systems? I&#x27;d love to see that because I&#x27;m having a very hard time believing that&#x27;s possible. Or maybe you can make something that will technically compile and function on any of those, but if it doesn&#x27;t perform reasonably then it&#x27;s really hard to call the portability aspect a success.<p>Also you&#x27;re very limited as to what you can actually do in ANSI C. You&#x27;re going to need to start poking directly at the hardware which is not going to be portable. Hell, even stuff like checking if a letter is within a certain range in the English alphabet might not work between machines. Letters might not be contiguous in the machine&#x27;s native character encoding.
gnufxover 3 years ago
I thought there&#x27;d be mention of CHERI as an up-to-the-minute architecture (as Arm Morello). I don&#x27;t remember whether it requires modifications to standard C, but there&#x27;s a C programming guide [1].<p>People who say everything is little endian presumably don&#x27;t maintain packages for the major GNU&#x2F;Linux systems which support s390x. I don&#x27;t remember how many big endian targets I could count in GCC&#x27;s list when I looked fairly recently. The place to look for &quot;weird&quot; features there is presumably the embedded systems processors.<p>1. <a href="https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-947.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-947.pdf</a>
viddiover 3 years ago
Haven&#x27;t read the article yet, but I have noticed that the tab keeps loading even after 10 minutes. Aborting the loading process leads to broken media.<p>I am no expert in HTML video delivery and haven&#x27;t tried it out, but maybe setting the preload attribute to &quot;none&quot; or &quot;metadata&quot; might help?
rjswover 3 years ago
There are C compilers for the PDP-10, it must count as fairly weird.
AnimalMuppetover 3 years ago
Overall a good article. I was a bit amused and&#x2F;or disgruntled to see a TRS80 in the &quot;Motorola 68000&quot; section, though...
评论 #30418975 未加载
ForOldHackover 3 years ago
No mention of the Interdata 8&#x2F;32 or the (cough cough) IBM RT?
评论 #30428499 未加载
enjoyyourlifeover 3 years ago
Why don&#x27;t C programmers always use stdint.h?