Skip to content

Instantly share code, notes, and snippets.

@flags
Last active December 14, 2015 22:49
Show Gist options
  • Save flags/5161201 to your computer and use it in GitHub Desktop.
Save flags/5161201 to your computer and use it in GitHub Desktop.
if (x >= y) #A label
x=x-y;
else
y=y-x
.data
x: word;
y: word;
//Machine Code
//Symbol table
Symbol Address
x 0000 0000 0101 (5)
y 0000 0000 0110 (6)
else 0000 0001 0011 (19)
then 0000 0001 0111 (23)
endif 0000 0001 1000 (24)
.instruction
LOD x //AC=x
SUB y //AC=x-y
JPO then //jump to then if AC >= 0, i.e., if x >= y
else:
LOD y //AC=y
SUB x // AC=y-x
STO y // y=AC=y-x
JMP endif //skip the then-part
then:
STO x //x=AC-x-y
endif: //code that follows the if statement
//Actual code binary machine code (OPCODE - ADDRESS)
LOD 5 0000 0000 0000 0101
SUB 6 0011 0000 0000 0110
JPO 23 0100 0000 0001 0111
LOD 6 0000 0000 0000 0110
SUB 5 0011 0000 0000 0101
STO 6 0001 0000 0000 0110
JMP 24 0110 0000 0001 1000
STO 5 0001 0000 0000 0101
//High-level
k = 5;
while (i <= 99) i = i+k;
//Asm
.data
k: word
i: word
.instruction
LOC 5 //AC=5
STO k //k=5
while:
LOC 99 //AC=99
SUB i //99-i
JNG exit //loop exits if i <= 99
loopbody:
LOD i //AC=i
ADD k //AC=i+k
STO i //i=AC=i+k
JMP while
exit (25):
Symbol table
Symbol Address
k 0000 0000 0101 (5)
i 0000 0000 0110 (6)
while 0000 0001 0010 (18)
exit 0000 0001 1001 (25)
Assembly code machine code
LOC 5 1011 0000 0000 0101
STO k 0001 0000 0000 0101
LOC 99 1011 0000 0110 0011
SUB i 0011 0000 0000 0110
JNG exit 0111 0000 0001 1001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment