Skip to content

Commit d1d4850

Browse files
authored
Merge pull request below#31 from gredman/system-call-fix
System call fixes from @gredman
2 parents 3228b59 + 58633f0 commit d1d4850

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

Chapter 07/fileio.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Various macros to perform file I/O
22

33
// The fd parameter needs to be a register.
4-
// Uses X0, X1, X8.
4+
// Uses X0-X3, X16.
55
// Return code is in X0.
66

77
#include <sys/syscall.h>
88

99
.equ O_RDONLY, 0
1010
.equ O_WRONLY, 1
11-
.equ O_CREAT, 0100
11+
.equ O_CREAT, 0x00000200
1212
.equ S_RDWR, 0666
1313
.equ AT_FDCWD, -2
1414

Chapter 07/main.S

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Assembler program to convert a string to
33
// all upper case by calling a function.
44
//
5-
// X0-X2, X8 - used by macros to call linux
5+
// X0-X2, X16 - used by macros to call kernel
66
// X11 - input file descriptor
77
// X9 - output file descriptor
88
// X10 - number of characters read
@@ -19,8 +19,8 @@
1919

2020
_start: MOV X0, #-1
2121
openFile inFile, O_RDONLY
22-
ADDS X11, XZR, X0 // save file descriptor
23-
B.PL nxtfil // pos number file opened ok
22+
MOV X11, X0 // save file descriptor (or error)
23+
B.CC nxtfil // carry clear, file opened ok
2424
MOV X1, #1 // stdout
2525
ADRP X2, inpErrsz@PAGE // Error msg
2626
ADD X2, X2, inpErrsz@PAGEOFF
@@ -29,8 +29,8 @@ _start: MOV X0, #-1
2929
B exit
3030

3131
nxtfil: openFile outFile, O_CREAT+O_WRONLY
32-
ADDS X9, XZR, X0 // save file descriptor
33-
B.PL loop // pos number file opened ok
32+
MOV X9 , X0 // save file descriptor (or error)
33+
B.CC loop // carry clear, file opened ok
3434
MOV X1, #1
3535
ADRP X2, outErrsz@PAGE
3636
ADD X2, X2, outErrsz@PAGEOFF

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ As we learned in Chapter 5, all assembler directives (like `.equ`) must be in lo
213213

214214
It is also important to notice that while the calls and definitions look similar, Linux and Darwin are not the same: `AT_FDCWD` is -100 on Linux, but must be -2 on Darwin.
215215

216+
Unlike Linux, errors are signified by setting the carry flag, and the error codes are non-negative. We therefore `MOV` the result into the required register instead of `ADDS` (we don't need to check for negative numbers, and need to preserve the condition flags) and B.CC to the success path.
217+
216218
## Chapter 8
217219

218220
This chapter is specifically for the Raspberry Pi 4, so there is nothing to do here.

0 commit comments

Comments
 (0)