15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

sub, similar to add.<br />

mul, and leq need some work.<br />

c○: Michael Kohlhase 291<br />

We will not go into detail for the other arithmetic commands, for example, mul could be implemented<br />

as follows:<br />

label instruction effect comment<br />

〈mul〉 dec IN2 SP: = SP − 1<br />

LOADI 0<br />

STORE 1 P (1): = 0 initialize result<br />

LOADIN 2 1 ACC : = P (SP + 1) read arg 1<br />

STORE 0 P (0): = ACC initialize counter to arg 1<br />

〈loop〉 JUMP= 〈end〉 if counter=0, we are finished<br />

LOADIN 2 0 ACC : = P (SP) read arg 2<br />

ADD 1 ACC : = ACC +P (1) current sum increased by arg 2<br />

STORE 1 P (1): = ACC cache result<br />

LOAD 0<br />

SUBI 1<br />

STORE 0 P (0): = P (0) − 1 decrease counter by 1<br />

JUMP loop repeat addition<br />

〈end〉 LOAD 1 load result<br />

STOREIN 2 0 push it on stack<br />

inc IN1<br />

JUMP 〈jt〉 back to jump table<br />

Note that mul and leq are the only two instruction whose corresponding piece of code is not<br />

of the unit complexity. 16 EdNote:16<br />

For the jump instructions, we do exactly what we would expect, we load the jump distance, add<br />

it to the register IN1, which we use to represent the VM program counter VPC. Incidentally, we<br />

can use the code for jp for the conditional jump cjp.<br />

Control Instructions<br />

label instruction effect comment<br />

〈jp〉 MOVE IN1 ACC ACC : = VPC<br />

STORE 0 P (0): = ACC cache VPC<br />

LOADIN 1 1 ACC : = P (VPC + 1) load i<br />

ADD 0 ACC : = ACC +P (0) compute new VPC value<br />

MOVE ACC IN1 IN1: = ACC update VPC<br />

JUMP 〈jt〉 jump back<br />

〈cjp〉 dec IN2 SP: = SP − 1 update for pop<br />

LOADIN 2 1 ACC : = P (SP + 1) pop value to ACC<br />

JUMP= 〈jp〉 perform jump if ACC = 0<br />

MOVE IN1 ACC otherwise, go on<br />

ADDI 2<br />

MOVE ACC IN1 VPC: = VPC + 2 point to next<br />

JUMP 〈jt〉 jump back<br />

c○: Michael Kohlhase 292<br />

The imperative stack operations use the index register heavily. Note the use of the offset 8 in the<br />

LOADIN , this comes from the layout of VM that uses the bottom eight cells in the data store as a<br />

scratchpad.<br />

16 EdNote: MK: explain this better<br />

163

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!