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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Hello World

509 点作者 fbrusch大约 1 年前

22 条评论

MuffinFlavored大约 1 年前
I got bored the other day and tried to achieve something similar on MacOS with Rust:<p><pre><code> #![no_std] #![no_main] use core::panic::PanicInfo; #[panic_handler] fn panic_handler(_panic: &amp;PanicInfo&lt;&#x27;_&gt;) -&gt; ! { &#x2F;&#x2F; TODO: write panic message to stderr write(2, &quot;Panic occured\n&quot;.as_bytes()); &#x2F;&#x2F; TODO: panic location + message unsafe { sc::syscall!(EXIT, 255 as u32) }; loop {} } fn write(fd: usize, buf: &amp;[u8]) { unsafe { sc::syscall!(WRITE, fd, buf.as_ptr(), buf.len()); } } #[no_mangle] pub extern &quot;C&quot; fn main() -&gt; u32 { write(1, &quot;Hello, world!\n&quot;.as_bytes()); return 0; } </code></pre> Then I inspected the ELF output in Ghidra. No matter what it was about ~16kb. I&#x27;m sure some code golf could be done to get it done (which has obviously been done + written about + documented before)
评论 #39975109 未加载
评论 #39977857 未加载
评论 #39972925 未加载
评论 #39977167 未加载
评论 #39975259 未加载
评论 #39973113 未加载
praptak大约 1 年前
There&#x27;s another rabbit hole which Musl seems to have skipped. Using `syscall` directly is not all there is to calling system functions on Linux.<p>The &quot;better behaved&quot; way is to call vDSO. It&#x27;s a magic mini-library which the kernel automatically maps into your address space. Thus the kernel is free to provide you with whatever code it deems optimal for doing a system call.<p>In particular some of the system calls might be optimized away and not require the `syscall` at all because they are executed in the userspace. Historically you could expect vDSO to choose between different mechanisms of calling the kernel (int 0x80, sysenter).<p><a href="https:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man7&#x2F;vdso.7.html" rel="nofollow">https:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man7&#x2F;vdso.7.html</a>
评论 #39980857 未加载
qweqwe14大约 1 年前
Also see this blog post, which compares &quot;Hello World&quot; programs in different languages by overhead: <a href="https:&#x2F;&#x2F;drewdevault.com&#x2F;2020&#x2F;01&#x2F;04&#x2F;Slow.html" rel="nofollow">https:&#x2F;&#x2F;drewdevault.com&#x2F;2020&#x2F;01&#x2F;04&#x2F;Slow.html</a><p>Follow-up: <a href="https:&#x2F;&#x2F;drewdevault.com&#x2F;2020&#x2F;01&#x2F;08&#x2F;Re-Slow.html" rel="nofollow">https:&#x2F;&#x2F;drewdevault.com&#x2F;2020&#x2F;01&#x2F;08&#x2F;Re-Slow.html</a><p>This legendary blogpost makes the smallest Linux program (the program simply exits with status 42): <a href="https:&#x2F;&#x2F;www.muppetlabs.com&#x2F;~breadbox&#x2F;software&#x2F;tiny&#x2F;teensy.html" rel="nofollow">https:&#x2F;&#x2F;www.muppetlabs.com&#x2F;~breadbox&#x2F;software&#x2F;tiny&#x2F;teensy.ht...</a><p>You can also find the smallest &quot;Hello World&quot; program on that website.
bkallus大约 1 年前
This almost entirely skips the role of the dynamic linker, which is arguably the true entry point of the program.<p>If you are interested in that argument, see <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;kenballus&#x2F;c7eff5db56aa8e4810d39021b23d8a8f" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;kenballus&#x2F;c7eff5db56aa8e4810d39021b2...</a>.
susam大约 1 年前
In case there are any DOS enthusiasts out here, a &quot;hello, world&quot; program written in assembly&#x2F;machine code in DOS used to be as small as 23 bytes: <a href="https:&#x2F;&#x2F;github.com&#x2F;susam&#x2F;hello">https:&#x2F;&#x2F;github.com&#x2F;susam&#x2F;hello</a><p>Out of these 23 bytes, 15 bytes are consumed by the dollar-terminated string itself. So really only eight bytes of machine code that consists of four x86 instructions.
cancerhacker大约 1 年前
I liked and appreciated this, two points I&#x27;d like to make: you should disable optimizations and whatever inlining caused printf to become puts (or, alternatively, write the hello world to use puts directly), second would be to break your compile step into the 4 real parts: preprocess, compile, assemble, link. Or add --save-temps to the cc line and describe the various files created. There&#x27;s a lot less magic involved if you can see the pipeline.
Syntaf大约 1 年前
This reminds me of one of my favorite CS assignments in college for a systems programming class:<p><pre><code> &gt; Given a hello world C++ snippet, submit the smallest possible compiled binary </code></pre> I remember using tools like readelf and objdump to inspect the program and slowly rip away layers and compiler optimizations until I ended up with the smallest possible binary that still outputted &quot;hello world&quot;. I googled around and of course found someone who did it likely much better than any of us students could have ever managed [1]<p>[1]: <a href="https:&#x2F;&#x2F;www.muppetlabs.com&#x2F;%7Ebreadbox&#x2F;software&#x2F;tiny&#x2F;teensy.html" rel="nofollow">https:&#x2F;&#x2F;www.muppetlabs.com&#x2F;%7Ebreadbox&#x2F;software&#x2F;tiny&#x2F;teensy....</a>
评论 #39972933 未加载
评论 #39975125 未加载
评论 #39977360 未加载
评论 #39975439 未加载
norir大约 1 年前
&gt; I’m sorry the ending maybe wasn’t as satisfying as you hoped. I’m happy someone found this interesting. I’m not quite sure why I wrote this, but it’s now after midnight so I should get some sleep.<p>This was actually a perfect ending to this piece.
delta_p_delta_x大约 1 年前
Sadly, like most &#x27;hello world&#x27; deep dives, the author stops at the `write` syscall and glosses over the rest. Everything before the syscall essentially boils down to `printf` calling `puts` calling `write`—it&#x27;s one function call after another forwarding the `char const*` through, with some book-keeping. In my opinion, not the most interesting.<p>What comes <i>after</i> the syscall is where everything gets very interesting and very <i>very</i> complicated. Of course, it also becomes much harder to debug or reverse-engineer because things get <i>very</i> close to the hardware.<p>Here&#x27;s a quick summary, roughly in order (I&#x27;m <i>still</i> glossing over; each of these steps probably has an entire battalion of software and hardware engineers from tens of different companies working on it, but I daresay it&#x27;s still more detailed than other &#x27;tours through &#x27;hello world&#x27;):<p><pre><code> - The kernel performs some setup setup to pipe the `stdout` of the hello world process into some input (not necessarily `stdin`; could be a function call too) of the terminal emulator process. - The terminal emulator calls into some typeface rendering library and the GPU driver to set up a framebuffer for the new output. - The above-mentioned typeface rendering library also interfaces with the GPU driver to convert what was so far just a one-dimensional byte buffer into a full-fledged two-dimensional raster image: - the corresponding font outlines for each character byte is loaded from disk; - each outline is aligned into a viewport; - these outlines are resized, kerning and font metrics applied from the font files set by the terminal emulator; - the GPU rasterises and anti-aliases the viewport (there are entire papers and textbooks written on these two topics alone). Rasterisation of font outlines may be done directly in hardware without shaders because nearly all outlines are quadratic Bézier splines. - This is a new framebuffer for the terminal emulator&#x27;s window, a 2D grid containing (usually) RGB bytes. - The windowing manager takes this framebuffer result and *composits* with the window frame (minimise&#x2F;maximise&#x2F;close buttons, window title, etc) and the rest of the desktop—all this is done usually on the GPU as well. - If the terminal emulator window in question has fancy transparency or &#x27;frosted glass&#x27; effects, this composition applies those effects with shaders here. - The resultant framebuffer is now at the full resolution and colour depth of the monitor, which is then packetised into an HDMI or DisplayPort signal by the GPU&#x27;s display-out hardware, depending on which is connected. - This is converted into an electrical signal by a DAC, and the result piped into the cable connecting the monitor&#x2F;internal display, at the frequency specified by the monitor refresh rate. - This is muddied by adaptive sync, which has to signal the monitor for a refresh instead of blindly pumping signals down the wire - The monitor&#x27;s input hardware has an ADC which re-converts the electrical signal from the cable into RGB bytes (or maybe not, and directly unwraps the HDMI&#x2F;DP packets for processing into the pixel-addressing signal, I&#x27;m not a monitor hardware engineer). - The electrical signal representing the framebuffer is converted into signals for the pixel-addressing hardware, which differs depending on the exact display type—whether LCD, OLED, plasma, or even CRT. OLED might be the most complicated since each *subpixel* needs to be *individually* addressed—for a 3840 × 2400 WRGB OLED as seen on LG OLED TVs, this is 3840 × 2400 × 4 = 36 864 000 subpixels, i.e. nearly 37 million pixels. - The display hardware refreshes with the new signal (again, this refresh could be scan-line, like CRT, or whole-framebuffer, like LCDs, OLEDs, and plasmas), and you finally see the result. </code></pre> Note that all this happens <i>at most</i> within the frame time of a monitor refresh, which is 16.67 ms for 60 Hz.
评论 #39972876 未加载
评论 #39974930 未加载
评论 #39972895 未加载
评论 #39989206 未加载
评论 #39975932 未加载
stevefolta大约 1 年前
&gt; Unlike python, however, you can’t just call an interpreter to run this program.<p>Sure you can: &quot;tcc -run hello.c&quot;. Okay, technically that&#x27;s an in-memory compiler rather than an interpreter.<p>For extra geek points, have your program say &quot;Hellorld&quot; instead of &quot;Hello world&quot;.
qznc大约 1 年前
Another interesting fact: Write a Hello World program in C++, run the preprocessor on it (g++ -E), then count the lines of the output.<p>I just tried and it shows me 33738 lines (744 lines for C btw).<p>In a language like C++, even Hello World uses like half of all the language features.
评论 #39974524 未加载
评论 #39974519 未加载
bitwize大约 1 年前
One of the things I really like about the &quot;hello, world&quot; example in K&amp;R is it&#x27;s used as a template to show the reader what a complete, working (if bare minimum) C program looks like. All the relevant parts are labeled including the #include preprocessor directive, function definition, function call (to printf), etc. There are a few paragraphs explaining these parts in greater detail. Finally some explanation is provided about how one might compile and run this program, using Unix as an example (the authors are pretty biased toward that system).<p>This article is very much in that same friendly, explanatory spirit, although obviously it goes into greater depth and uses a modern system.
评论 #39979713 未加载
sohzm大约 1 年前
This reminded me of this video &quot;Advanced Hello World in Zig - Loris Cro&quot; <a href="https:&#x2F;&#x2F;youtu.be&#x2F;iZFXAN8kpPo" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;iZFXAN8kpPo</a> although not a equivalent comparison but still a intresting watch
baudaux大约 1 年前
GNU hello program is quite complex as well<p><a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;hello&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;hello&#x2F;</a>
评论 #39978013 未加载
Bengalilol大约 1 年前
Just saying hi ! (and thanks for the read, got me back to the old days of ASM One and SEKA on Amiga, trying to clean up my memory dust pile)
评论 #39974872 未加载
mseepgood大约 1 年前
Could have continued with following the syscall code in the kernel.
deepsun大约 1 年前
Would be cool to see the same for statically-linked HelloWorld.
ben_w大约 1 年前
I&#x27;ve been thinking recently, we might have too many layers of abstraction in our systems.<p>And I didn&#x27;t even know about most of the ones in this post.
评论 #39972997 未加载
评论 #39972953 未加载
评论 #39973271 未加载
kenneth大约 1 年前
What I think is shocking is that 99% of the software engineering world these days would have zero ability to comprehend this explanation. In my opinion, you could not be a real programmer without the ability to understand what your program is under the hood. You might be able to get away with it, but you&#x27;re just faking it.<p>And yet, 99% of the people I&#x27;ve ever seen in the industry have no idea how any of the code they write works.<p>I used to ask a simple interview question, I wanted to see if potential hires could explain what a pointer or memory was. Few ever could.
评论 #39979255 未加载
Retr0id大约 1 年前
&gt; All modern big and important programs that make a computer work are written this way [AoT-compiled to native code].<p>This is the conventional wisdom, but it&#x27;s increasingly not true.
评论 #39974711 未加载
nektro大约 1 年前
loved it! except the final conclusion
vivzkestrel大约 1 年前
there was another post like this on HN that was actually illustrated in a better manner, anyone got links to it?