📄 chapter 7 code generation for control statements.htm
字号:
LGA 00000008 <B>j:= 3;</B>
LID 00000003
SSD 00000000
JMP SelectEnd
SelectStart:
CASE _s32 0003
Lo: 00000001 Hi: 0000000A Default: CaseElse
00000001 to 00000003: Case01
00000005 to 00000005: Case01
00000009 to 0000000A: Case02
SelectEnd: <B>end select</B>
RTN
*** Test 2 ******************************************
program TestCase1;
var
i, j: int;
begin
i:= 4;
j:= 3;
select i from
case 1 to 3, 5:
j:= 1;
case 9 to 10:
j:= 2;
end select;
end program.
*** DISASSEMBLY *************************************
ENTR 00000000
LGA 00000004
TS
SJPZ begin
RTN
begin:
LGA 00000004 <B>i:= 4;</B>
LID 00000004
SSD 00000000
LGA 00000008 <B>j:= 3;</B>
LID 00000003
SSD 00000000
LGD 00000004 <B>select i from</B>
JMP SelectStart
Case01: <B>case 1 to 3, 5:</B>
LGA 00000008 <B>j:= 1;</B>
LID 00000001
SSD 00000000
JMP SelectEnd
Case02: <B>case 9 to 10:</B>
LGA 00000008 <B>j:= 2;</B>
LID 00000002
SSD 00000000
JMP SelectEnd
SelectStart:
CASE _s32 0003
Lo: 00000001 Hi: 0000000A Default: SelectEnd02
00000001 to 00000003: Case01
00000005 to 00000005: Case01
00000009 to 0000000A: Case02
SelectEnd: <B>end select</B>
RTN
</PRE><!--
<pre>
<b> program TestCase;
var
i: int;
f: bool;
begin
//*** Test 1 *************************
write "Testing case with else:\n";
for i:= 0 to 11 do
write "\ti = ", i, ": ";
select i from
case 1 to 3, 5:
write "jumped to case of 1 to 3, 5.\n";
case 9 to 10:
write "jumped to case of 9 to 10.\n";
else
write "jumped to case else.\n";
end select;
loop;
//*** Test 2 *************************
write "\n\nTesting case with no else:\n";
for i:= 0 to 11 do
write "\ti = ", i, ": ";
f:= true;
select i from
case 1 to 3, 5:
f:= false;
write "jumped to case of 1 to 3, 5.\n";
case 9 to 10:
f:= false;
write "jumped to case of 9 to 10.\n";
end select;
if f then
write "no case selected\n";
end if;
loop;
end program.
</b>
*** DISASSEMBLY *************************************
ENTR 00000000
LGA 00000004
TS
SJPZ begin
RTN
begin:
LSTA 00000000 <b>write "Testing case with else:\n";</b>
SYS 00
LGA 00000009 <b>for i:= 0 to 11 do</b>
LID 00000000
LID 0000000B
LID 00000001
FOR _s32 SkipFor01
ForStart01:
LSTA 00000019 <b>write "\ti = ", i, ": ";</b>
SYS 00
LGD 00000004
LID 00000000
LIB 14
SYS 02
LSTA 0000001F
SYS 00
LGD 00000009 <b>select i from</b>
JMP SelectStart01
Case0101: <b>case 1 to 3, 5:</b>
LSTA 00000022 <b>write "jumped to case of 1 to 3, 5.\n";</b>
SYS 00
JMP Select01End01
Case0102: <b>case 9 to 10:</b>
LSTA 00000040 <b>write "jumped to case of 9 to 10.\n";</b>
SYS 00
JMP Select01End01
SelectElse01: <b>else</b>
LSTA 0000005C <b>write "jumped to case else.\n";</b>
SYS 00
JMP Select01End01
SelectStart01:
CASE _s32 0003
Lo: 00000001 Hi: 0000000A Default: SelectElse01
00000001 to 00000003: Case0101
00000005 to 00000005: Case0101
00000009 to 0000000A: Case0102
SelectEnd01: <b>end select</b>
NEXT _s32 ForStart01: <b>next</b>
SkipFor01:
LSTA 00000072 <b>write "\n\nTesting case with no else:\n";</b>
SYS 00
LGA 00000009 <b>for i:= 0 to 11 do</b>
LID 00000000
LID 0000000B
LID 00000001
FOR _s32 SkipFor02
LSTA 00000090 <b>write "\ti = ", i, ": ";</b>
SYS 00
LGD 00000009
LID 00000000
LIB 14
SYS 02
LSTA 00000096
SYS 00
LGA 00000008 <b>f:= true;</b>
LIB 01
SSB 00000000
LGD 00000009 <b>select i from</b>
JMP SelectStart02
Case0201: <b>case 1 to 3, 5:</b>
LGA 0000000D <b>f:= false;</b>
LIB 00
SSB 00000000
LSTA 00000099 <b>write "jumped to case of 1 to 3, 5.\n";</b>
SYS 00
JMP 008E226B
Case0202: <b>case 9 to 10:</b>
LGA 0000000D <b>f:= false;</b>
LIB 00
SSB 00000000
LSTA 000000B7 <b>write "jumped to case of 9 to 10.\n";</b>
SYS 00
JMP 008E226B
SelectStart02:
CASE _s32 0003
Lo: 00000001 Hi: 0000000A Default: SelectEnd02
00000001 to 00000003: Case0201
00000005 to 00000005: Case0201
00000009 to 0000000A: Case0202
SelectEnd02: <b>end select</b>
LGB 00000008 <b>if f then</b>
JPZ EndIf01
LSTA 000000D3 <b>write "no case selected\n";</b>
SYS 00
EndIf01: <b>end if;</b>
NEXT _s32 FFFFFF53
SkipFor02: <b>loop;</b>
RTN
</pre>
-->
<P>
<P>
<P><!----------------------------------------------------------------------------->
<H2>7.5 Input and Output, System calls</H2><!----------------------------------------------------------------------------->Input
and output in the virtual machine is handled through a series of system-level
instructions. The <TT>SYS</TT> instruction, patterened after a hardware
interrupt is the method for accessing the VM's native BIOS. The BIOS for the SAL
VM is fairly small, and only consists of a handfull of instructions.
<P>
<H3>7.5.1 The <TT>SYS</TT> Instruction</H3>The SYS instruction takes two
immediate parameters; each parameter is one byte long. The first parameter
designates what type of service is being requested, and the second parameter
requests a particular function within a service. For instance, in order to print
out "Hello, world!\n" to the screen, a program would use the console service 00,
and the function to write a null-terminated string, 00. <PRE> -------- Service requested
|
| ----- Function within the service
| |
V V
SYS 00 00 ; The sys call to write a null-terminated string to the console
<B>Figure {SYSANAT}.</B> The anatomy of a SYS call
</PRE><I>Note that as of this writing, only a few of the selected sys calls have
been implemented, namely, the ones that have to do with input and output,
copying memory, and floating point exponents.</I>
<P>Consistant with most hardware architectures, the EES is drawn upside down. It
expands downward, and the top is at the bottom. <PRE> EES
--------
| val |
|--------|
| val |
|--------|
| val | <-- Top of EES
|--------|
| . . | | EES expands downward
|
V
</PRE>Here is a listing of the system calls that have been implemented, and an
explanation of how each one works:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -