Listing 11.2 A sorting routine
routine Sort(a: [int], size: int) do
for i in 1..size loop
for j in 0..i reverse loop
if a[j-1] < a[j] then
int w := a[j-1]
a[j-1] := a[j]
a[j] := w
end
end
end
end Sort
Listing 11.3 The sorting program written in the assembly language
// i: R1; j: R2; intermediate values: R3, R4, R5
R15 := Size; // ‘Size’ labels the data block below
R15 := *R15;
R16 := Array; // ‘Array’ labels the data block below
trace R15,R16;
R1 := 1;
// i := 1
R3 := Size;
R3 := *R3;
// R3: Size
R4 := 1;
R3 -= R4;
R3 ?= R1;
// Compare Size-1 and i
R4 &= R3;
// Extract > sign using R4=1 as mask for >
R3 := OutOuter;
if R4 goto R3; // if i>Size goto OutOuter
// Organize inner loop
R2 := R1; NOP; // j := i
R3 := 0;
// w := 0
R3 ?= R2;
// Compare j with 0
R4 := 4;
// Mask for equality
R3 &= R4;
// Extract equality sign
R4 := OutInner;
if R3 goto R4; // if j=0 exit the inner loop
11.3 Low Level: The Assembly Language
169
routine Sort(a: [int], size: int) do
for i in 1..size loop
for j in 0..i reverse loop
if a[j-1] < a[j] then
int w := a[j-1]
a[j-1] := a[j]
a[j] := w
end
end
end
end Sort
Listing 11.3 The sorting program written in the assembly language
// i: R1; j: R2; intermediate values: R3, R4, R5
R15 := Size; // ‘Size’ labels the data block below
R15 := *R15;
R16 := Array; // ‘Array’ labels the data block below
trace R15,R16;
R1 := 1;
// i := 1
R3 := Size;
R3 := *R3;
// R3: Size
R4 := 1;
R3 -= R4;
R3 ?= R1;
// Compare Size-1 and i
R4 &= R3;
// Extract > sign using R4=1 as mask for >
R3 := OutOuter;
if R4 goto R3; // if i>Size goto OutOuter
// Organize inner loop
R2 := R1; NOP; // j := i
R3 := 0;
// w := 0
R3 ?= R2;
// Compare j with 0
R4 := 4;
// Mask for equality
R3 &= R4;
// Extract equality sign
R4 := OutInner;
if R3 goto R4; // if j=0 exit the inner loop
11.3 Low Level: The Assembly Language
169
