I've been working through Programming from the Ground Up book on an old Dell Dimension PC, bought circa 2006, with 32-bit architecture and running a 32-bit Xubuntu installation. I enjoyed the first 6 chapters, and feel like I am 'getting' assembly language so far and giving myself a decent foundation for further study.
However, I'm stuck on the Chapter 7 example (page 125 as regards what's displayed in the book; page 131 as the PDF reader considers it to be). Specifically, when I add my own non-conditional jump immediately after the open call to allow me to move the value of %eax to %ebx and therefore inspect it with echo $?:
- When the expected input file does exist, the call to Linux call #5 (open) returns 3 consistently. This seems to make sense: a successful call, should, based on a successful open, return a non-zero file descriptor.
- The problem: when the input file does not exist, the Linux call #5 returns 254. Based on what I've read in the book I am working on, plus some man page checks and Googling, I would have expected a negative value. This should have caused the program, as written in the book, to throw an error due to a negative value in %eax; but that doesn't happen.
In setting up the 'does not exist' scenario, I have tried the following tweaks as regards the input file:
- Renamed it with a '.mv suffix'.
- Moved it to the Waste Basket.
- Adjusted the source file add-year.s to expect a totally different input file with a long, almost-certainly-unique name which couldn't be expected to exist elsewhere on my file system.
In all 3 cases, system call #5 returns 254. I wondered, perhaps if it doesn't find the file, it creates it? But the book in front of me states that moving the literal $0 into register %ecx instructs system call #5 to open a file for read-only.
From my Googling, it seems that assembly language directly hitting system calls is not the normal way to do things; in practice, programmers would call Linux C wrapper programs from C. This makes sense - and I'm keen to learn C in the future - however, it would be great if anyone who's worked through this book (or is just a wizard anyway) can help with this specific error, to help myself (and hopefully other readers of this post) work through this book's examples without having to admit defeat.