Hardware Reference
In-Depth Information
Lines 5, 18 and 20 contain labels, which are indicated by a colon :. These
labels represent numerical values, similar to constants. In this case, however, the
assembler has to determine the numerical values. Since start is at the beginning of
the TEXT section, its value will be 0, but the value of any subsequent labels in the
TEXT section (not present in this example), would depend on how many bytes of
code preceded them. Now consider line 6. This line ends with the difference of
two labels, which is numerically a constant. Thus, line 6 is effectively the same as
MOV CX,12
except that it lets the assembler determine the string length, rather than making the
programmer do it. The value indicated here is the amount of space in the data
reserved for the string on line 19. The MOV on line 6 is the copy command, which
requires the de
hw to be copied to CX .
_EXI T =
1
!
1
CS: 00 DS=SS=ES: 002
AH:00 AL:0c AX: 12
BH:00 BL:00 BX: 0
CH:00 CL:0c CX: 12
DH:00 DL:00 DX: 0
SP: 7fd8 SF O D S Z C =>
BP: 0000 CC - > p -
MOV CX,de-hw
! 6
_WRITE = 4
! 2
PUSH CX
!
7
_STDOUT = 1
! 3
PUSH HW
!
8
.SECT .TEXT
! 4
PUSH _STDOUT
! 9
start:
! 5
PUSH _WRITE
! 10
MOV
CX,de-hw
! 6
0004
0001 =>
0000
000c
SYS
! 11
PUSH CX
! 7
-
ADD
SP , 8
! 12
PUSH hw
! 8
S I : 0000
IP:000c:PC
SUB CX,AX
! 13
PUSH _STDOUT
! 9
DI : 0000
start + 7
PUSH CX
! 14
PUSH _WRITE
! 1 0
E
I
SYS
! 1 1
ADD
SP,
8
!
12
hw
SUB
CX,AX
! 1 3
> Hello World\n
PUSH CX
! 1 4
PUSH _EXIT
!
15
hw + 0 = 0000: 48 65 6c 6c 6f 20 57 6f Hello World 25928
SYS
! 1 6
.SECT .DATA
! 1 7
hw:
! 1 8
"
!
. ASCII
"Hello World\n
19
de: .BYTE
0
!
20
(a)
(b)
Figure C-12. (a) HlloWrld.s. (b) The corresponding tracer window.
Lines 7 through 11 show how system calls are made in the toolkit. These five
lines are the assembly code translation of the C language function call
write(1, hw, 12);
where the first parameter is the file descriptor for standard output (1), the second is
the address of the string to be printed ( hw ), and the third is the length of the string
(12). Lines 7 through 9 push these parameters onto the stack in reverse order,
which is the C calling sequence and the one used by the tracer. Line 10 pushes the
system call number for write (4) onto the stack, and line 11 makes the actual call.
While this calling sequence closely mimics how an actual assembly language pro-
gram would work on a UNIX (or Linux) PC, for a different operating system, it
would have to be modified slightly to use the calling conventions of that operating
Search WWH ::




Custom Search