#include <iostream>#include <stdlib.h>#define MAP_WIDTH 20#define MAP_HEIGHT 20//ASM needs strings#define ASM_MAP_WIDTH "$20"#define ASM_MAP_HEIGHT "$20"void fill(int x, int y);//char * map;char map[MAP_WIDTH * MAP_HEIGHT];int main() { //X 0123456789.... Ychar * map2= "####################" //0 "#******************#" //1 "#******************#" //2 "#******************#" //3 "#*******#######****#" //4 "#*******#*****#****#" //5 "#*******#*#####****#" //6 "#*******#*#********#" //.. "#*******#*#####****#" //.. "#*******#******#***#" "#*******#*###*#****#" "#*******#*#*#**#***#" "#***#####*#**##****#" "#***#*****#********#" "#***#######********#" "#******************#" "#******************#" "#******************#" "#******************#" "####################"; memcpy (map,map2,strlen(map2)); fill(9,9); for (int i = 0; i < MAP_HEIGHT; i++) { char * line = (char *) malloc(MAP_WIDTH + 2); line[MAP_WIDTH] = '\n'; line[MAP_WIDTH + 1] = '\0'; memcpy(line, map + i * MAP_WIDTH, MAP_WIDTH); printf(line); } return 0;}int asm_start; //you can only use global variables in in-line assembly...//Pre: the area is limited (else expect some nast crap to happen with stack pointers etc.)void fill(int x, int y) { //Basic idea: start at point (x,y) and fill up the map from there //Calculate starting position in map-array asm_start = x + y * MAP_WIDTH + (unsigned int) ↦ asm ("movl _asm_start, %eax"); asm ("pushl %eax"); //Push value onto stack asm ("movl $1, %edx"); //Number of to-be-checked positions in the stack (stack counter) asm ("movb $35, %cl"); //'#' = ASCII 35 //Starting point inserted into the stack, now start the recursive function: asm ("fillNext:"); asm ("popl %eax"); //Get the position out of the stack asm ("movb (%eax), %bl"); //Get the value of the position ('#'=ASCII 35 or '*'=ASCII 42 asm ("cmpb %cl, %bl"); //Compare map[position] to '#' asm ("je next"); //If that position is already '#', then don't do anything //Set map to '#' asm ("movb %cl, (%eax)"); //Check adjacent fields asm ("decl %eax"); //1:fill(x-1,y) asm ("pushl %eax"); asm ("addl $2, %eax"); //2:fill(x+2,y) asm ("pushl %eax"); asm ("decl %eax"); asm ("subl " ASM_MAP_WIDTH ", %eax"); asm ("pushl %eax"); //3:fill(x,y-1) asm ("addl " ASM_MAP_WIDTH ", %eax"); asm ("addl " ASM_MAP_WIDTH ", %eax"); asm ("pushl %eax"); //4:fill(x,y+1) asm ("addl $4, %edx"); asm ("next:"); asm ("decl %edx"); //Decrement the stack counter asm ("cmpl $0, %edx"); asm ("jne fillNext"); //If there are more map positions to be done, then go on //At this point, everything has been filled up}
Note that, because of using the stack, the area is restricted to some size. Not sure what that size is though... I've tried a space up to 20000 and it still didn't give problems (probably because it only has to hold values for the edges :))