Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Created July 30, 2013 17:30
Show Gist options
  • Select an option

  • Save calebhearth/6115022 to your computer and use it in GitHub Desktop.

Select an option

Save calebhearth/6115022 to your computer and use it in GitHub Desktop.
void calculate() {
unsigned int neighbors;
for (int m = 0; m < H; m++) {
for (int n = 0; n < W; n++) {
neighbors = 0;
//Begin counting number of neighbors:
// 123
// 4x6 Array locations for ease of reading
// 789
_asm{
mov ebx, W
CHECKTOPLEFT:
cmp m, 0
ja CHECKTOPLEFT2 // Skip border check and check neighbor top left
// "else"
jmp CHECKTOP // Skip border check and check for neighbor above
CHECKTOPLEFT2:
cmp n, 0
ja TOPLEFTINC // increment neighbors
// "else"
jmp CHECKTOP // Check neighbor above, skip border check
TOPLEFTINC: // Increment neighbors for current space
// Get the address of m-1, n-1
mov eax, m
dec eax
mul bx // Convert to array coordinates
add eax, n
dec eax
cmp now[eax * 4], 0
jna CHECKTOP // Check neighbor above
// "else"
inc neighbors
CHECKTOP: // Check space 2
cmp m, 0
ja TOPINC
// "else"
jmp CHECKTOPRIGHT // Check for neighbor to top right
TOPINC: // Increment neighbors for current space
// Get the address of m-1, n
mov eax, m
dec eax
mul bx
add eax, n
cmp now[eax * 4], 0
jna CHECKTOPRIGHT // Check for neighbor to top right
// "else"
inc neighbors
CHECKTOPRIGHT: // Check for border
cmp m, 0
ja CHECKTOPRIGHT2 // Check for neighbor to top right
// "else"
jmp CHECKLEFT // Check for neighbor to left
CHECKTOPRIGHT2: // Check neighbor top right
cmp m, W
jb TOPRIGHTINC
// "else"
jmp CHECKLEFT
TOPRIGHTINC: // Increment neighbors for current space
// Get the address of m-1, n+1
mov eax, m
dec eax
mul bx
add eax, n
inc eax
cmp now[eax * 4], 0
jna CHECKLEFT // Check neighbor to left
// "else"
inc neighbors
CHECKLEFT: // Check neighbor to left
cmp n, 0
ja LEFTINC
// "else"
jmp CHECKRIGHT // Check neighbor to right
LEFTINC: // Increments neighbors for current space
// Get the address of m, n-1
mov eax, m
mul bx
add eax, n
dec eax
cmp now[eax * 4], 0
jna CHECKRIGHT // Check neighbor to right
// "else"
inc neighbors
CHECKRIGHT: // Check for neighbors to right
cmp n, W
jb RIGHTINC // Increment neighbors
// "else"
jmp CHECKBOTTOMLEFT // Check for neighbors bottom left
RIGHTINC:
// Get the address of m, n+1
mov eax, m
mul bx
add eax, n
inc eax
cmp now[eax * 4], 0
jna CHECKBOTTOMLEFT
// "else"
inc neighbors
CHECKBOTTOMLEFT:
cmp m, H
jb CHECKBOTTOMLEFT2 // Check for neighbors bottom left
// "else"
jmp CHECKBOTTOM
CHECKBOTTOMLEFT2: // Check for edge
cmp n, 0 // if m = H then the space is on the edge
ja BOTTOMLEFTINC // if not on the edge
// "else"
jmp CHECKBOTTOM // otherwise,
BOTTOMLEFTINC: // Check for neighbors bottom left
// Get the address of m+1, n-1
mov eax, m
inc eax
mul bx
add eax, n
dec eax
cmp now[eax * 4], 0
jna CHECKBOTTOM
// "else"
inc neighbors
CHECKBOTTOM: // Check for border
cmp m, H
jb BOTTOMINC
// "else"
jmp CHECKBOTTOMRIGHT
BOTTOMINC: // increment neghbors
// Get the address of m+1, n
mov eax, m
inc eax
mul bx
add eax, n
cmp now[eax * 4], 0
jna CHECKBOTTOMRIGHT
// "else"
inc neighbors
CHECKBOTTOMRIGHT:
cmp m, H
jb CHECKBOTTOMRIGHT2
// "else"
jmp DONEFINDINGNEIGHBORS
CHECKBOTTOMRIGHT2:
cmp n, W
jb BOTTOMRIGHTINC
// "else"
jmp DONEFINDINGNEIGHBORS
BOTTOMRIGHTINC:
// Get the address of m+1, n+1
mov eax, m
inc eax
mul bx
add eax, n
inc eax
cmp now[eax * 4], 0
jna DONEFINDINGNEIGHBORS
// "else"
inc neighbors
DONEFINDINGNEIGHBORS:
}
//Apply rules to the cell:
_asm{
// set eax to the index of current cell:
mov ebx, W
mov eax, m
mul bx
add eax, n
LONLINESS: // If less than 2 neighbors, kill
//if (now[m][n] == 1 && neighbors < 2)
cmp now[eax * 4], 0
jna BIRTH
cmp neighbors, 2
jb DIE
CROWDING: // If more than 3 neighbors, kill
//else if (now[m][n] == 1 && neighbors > 3)
cmp now[eax * 4], 0
jna BIRTH
cmp neighbors, 3
ja DIE
LIVEON: // If exactly 2 or 3 neighbors and alive, no change
//else if (now[m][n]==1&&(neighbors == 2 || neighbors == 3))
cmp now[eax * 4], 0
jna BIRTH
cmp neighbors, 2
je LIVE
cmp neighbors, 3
je LIVE
BIRTH: // If 3 neighbors and dead, new life
//else if (now[m][n] == 0 && neighbors == 3)
cmp now[eax * 4], 0
ja DONE
cmp neighbors, 3
je LIVE
DIE: // logic to kill
mov next[eax * 4], 0
jmp DONE
LIVE: // logic to live/bear life
mov next[eax * 4], 1
jmp DONE
DONE:
// DONE
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment