User Tools

Site Tools


user:lleber:start

Input Validator

  ;
  ;~Zilent~ Team FoFF ex-member {Fight Only For Freedom}
  ;This module is supposed to be the first line of defense against
  ;those damned end-users who will stop at nothing to invalidate your program
  ;
  ;	ARGS:
  ;		EAX - The input type expected from the end-user
  ;			0x0 - Unsigned 32 Bit Integer
  ;			0x1 - Signed 32 Bit Integer
  ;			0x2 - Unsigned 16 Bit Integer
  ;			0x3 - Signed 16 Bit Integer
  ;			0x4 - Signed Byte
  ;			0x5 - Unsigned Byte
  ;			0x6 - Character
  ;
  ;	RETURN:
  ;		EAX - The end-user's input
  ;
  %define KERNEL		0x80							;Reference to SYS_CALL
  %define SYS_READ	0x3								;Refreence to SYS_READ
  %define SYS_WRITE	0x4								;Reference to SYS_WRITE
  %define	UINT32	0x0									;Unsigned 32 bit integer
  %define SINT32	0x1									;Signed 32 bit integer
  %define UINT16	0x2									;Unsigned 16 bit integer
  %define SINT16	0x3									;Signed 16 bit integer
  %define SBYTE	0x4									;Signed Byte
  %define UBYTE	0x5									;Unsigned Byte
  %define CHAR	0x6									;Char
  %define	INT32BUCKETS	0xA							;The 'Buckets' needed for an INT32
  %define INT16BUCKETS	0x5							;The 'Buckets' needed for an INT16
  %define BYTEBUCKETS		0x3							;The 'Buckets' needed for a Byte
  %define CHARBUCKETS		0x3							;The 'Buckets' needed for a Char
  %define SYS_IOCTL		0x36						;A reference to the SYS_IOCTL call code
  %define TCGETA			0x5405						;An argument for IO_CTL
									;This argument GETS the termios status
  %define TCSETA			0x5406						;An argument for IO_CTL
												;This argument SETS the termios status
  %define STDIN_FILENO	0							;
  %define ICANON			0x2							;This is the bit for canonical input processing mode
  %define ECHO			0x8							;This is the bit for echoing of input characters
  %define ECHOE			0x10						;This is the bit for erasing the last input character if erased
  %define ECHOK			0x20						;This is the bit for displaying the KILL character
  %define ECHONL			0x40						;This is the bit for displaying the newline character
  segment .data
  HEADERTXT		db	"Please enter", 0
  HEADERLEN		equ	$-HEADERTXT
  UINT32TXT		db	" a 4 Byte Unsigned Integer(0 <-> 4,294,967,295) ", 0
  UINT32LEN		equ	$-UINT32TXT
  SINT32TXT		db	" a 4 Byte Signed Integer( -2,147,483,648 <-> 2,147,483,647) ", 0
  SINT32LEN		equ	$-SINT32TXT
  UINT16TXT		db	" a 2 Byte Unsigned Integer (0 <-> 65535) ", 0
  UINT16LEN		equ	$-UINT16TXT
  SINT16TXT		db	" a 2 Byte Signed Integer(-32768 <-> 32767) ", 0
  SINT16LEN		equ	$-SINT16TXT
  SBYTETXT		db	" a Signed Byte (-128 <-> 127) ", 0
  SBYTELEN		equ	$-SBYTETXT
  UBYTETXT		db	" an Unsigned Byte (0 <-> 255) ", 0
  UBYTELEN		equ	$-UBYTETXT
  CHARTXT			db	" a Character (a-z, A-Z, 0-9) ", 0
  CHARLEN			equ	$-CHARTXT
  ERRORTXT		db	" You supplied an incorrect argument, dumbass, oops, etiquette, I forgot.", 0
  ERRORLEN		equ	$-ERRORTXT
  segment .bss
  save_t				resb	2			;This will save the initial Termios flag bits
  struct_t			resb	17			;This represents the Termios structure that we use for toggling input
inputType			resb	1			;Stores arg[0]
activeBuckets		resb	1			;Stores the amount of active 'buckets'
returnValue			resb	10			;Stores the end-user's input
calculatedValue		resd	1			;Stores the end-user's calculated input
  segment .text
global _asm_main
  _asm_main:
enter 0, 0						;OEP
pusha							;Push all registers onto the stack
  beginRoutine:	
mov word [inputType], 0x0		;eax, eventually
mov edx, HEADERLEN				;Move the length of the etiquette 'string' into edx
mov ecx, HEADERTXT				;Move the address of the first character in the etiquette 'string' into ecx
mov ebx, 0						;Move 0 into ebx for some reason? The API calls for it, I don't care why really
mov eax, SYS_WRITE				;Move the 'call code' into eax
int KERNEL						;Invoke the kernel
mov eax, [inputType]			;Bring the user's argument into eax
cmp eax, UINT32					;Does the user want a 4 byte unsigned integer?
jne a1							;No? Jump it.
mov ecx, UINT32TXT				;Yes? Move the integer4Byte message into ecx
mov edx, UINT32LEN				;Move the integer4Byte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a1:
cmp eax, SINT32					;Does the user want a 4 byte signed integer?
jne a2							;No? Jump it.
mov ecx, SINT32TXT				;Yes? Move the integer4SByte message into ecx
mov edx, SINT32LEN				;Move the integer4SByte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a2:
cmp eax, UINT16					;Does the user want a 2 byte unsigned integer?
jne a3							;No? Jump it.
mov ecx, UINT16TXT				;Yes? Move the integer2Byte message into ecx
mov edx, UINT16LEN				;Move the integer2Byte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a3:
cmp eax, UINT16					;Does the user want a 2 byte signed integer?
jne a4							;No? Jump it.
mov ecx, SINT16TXT				;Yes? Move the integer2SByte message into ecx
mov edx, SINT16LEN				;Move the integer2SByte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a4:
cmp eax, SBYTE					;Does the user want a signed byte?
jne a5							;No? Jump it.
mov ecx, SBYTETXT				;Yes? Move the sByte message into ecx
mov edx, SBYTELEN				;Move the sByte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a5:
cmp eax, UBYTE					;Does the user want an unsigned byte?
jne a6							;No? Jump it.
mov ecx, UBYTETXT				;Yes? Move the uByte message into ecx
mov edx, UBYTELEN				;Move the uByte message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a6:
cmp eax, CHAR					;Does the user want a character?
jne a7							;No? Jump it.
mov ecx, CHARTXT				;Yes? Move the char message into ecx
mov edx, CHARLEN				;Move the char message length into edx
jmp printMessage				;Jump out of this ugly 'switch' structure like monster
  a7:
mov ecx, ERRORTXT				;Some users are impossible to please.  Let's send them a nasty error message.
mov edx, ERRORLEN				;Move the error message length into edx
jmp error		 				;Jump out of this ugly 'switch' structure like monster
  printMessage:
mov ebx, 1						;Blame the documentation ^_^
mov eax, SYS_WRITE				;Move the 'call code' into eax
int KERNEL						;Invoke the kernel
  saveTermiosState:
xor eax, eax					;Set eax to 0
mov al, SYS_IOCTL				;Move our SYS_IOCTL reference into al
xor ebx, ebx					;Set ebx to 0
mov ecx, TCGETA					;Move the GET termios status argument into ecx
lea edx, [struct_t]				;Calculate the effective address of the termios structure
int KERNEL						;Invoke the kernel
mov ax, [struct_t + 6]			;Move the 16 flag bits to save into ax
mov [save_t], ax				;Tuck the 16 flag bits into memory location 'save_t'
  killBufferedInput:
xor eax, eax										;Set eax to 0
mov ax, [save_t]									;Copy the flag bits to ax
and ax, ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL)	;mAnIpUlAtE tHeM mAwHaHa
and ax, ~(ECHO | ECHOE | ECHOK | ECHONL)			;MaNiPuLaTe ThEm MaWhAhA
mov [struct_t + 6], ax								;Store the new bits at memory address save_t + 6
xor ebx, ebx										;Set ebx to 0
mov ecx, TCSETA										;Move the argument to SET termios into ecx
  inputRouter:
mov eax, [inputType]
cmp eax, UINT32					;Recieving UINT32?
jne b1							;No? Jump it.
mov edx, INT32BUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b1:
cmp eax, SINT32					;Recieving SINT32?
jne b2							;No? Jump it.
mov edx, INT32BUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b2:
cmp eax, UINT16					;Recieving UINT16?
jne b3							;No? Jump it.
mov edx, INT16BUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b3:
cmp eax, SINT16					;Recieving SINT16?
jne b4							;No? Jump it.
mov edx, INT16BUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b4:
cmp eax, SBYTE					;Recieving SBYTE?
jne b5							;No? Jump it.
mov edx, BYTEBUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b5:
cmp eax, UBYTE					;Recieving UBYTE?
jne b6							;No? Jump it.
mov edx, BYTEBUCKETS			;Yes? Load the proper buckets
jmp recieveInput				;Jump to recieveInput
  b6:
mov edx, [CHARBUCKETS]			;Else, we must be wanting a char, unless some nutwad is screwing with our memory outside.
  recieveInput:
mov [activeBuckets], edx		;Store the buckets for a bit later
xor eax, eax					;Set eax to 0
mov al, SYS_READ				;Move the 'call code'
xor ebx, ebx					;Set ebx to 0
mov bl, STDIN_FILENO			;Move STDIN_FILENO
mov ecx, returnValue			;Move the buffer into ecx
int KERNEL						;Invoke the kernel
  resetTermios:
mov ax, [save_t]				;Move our flag bits into ax
mov al, SYS_IOCTL				;Move the SYS_IOCTL reference into al
xor ebx, ebx					;Set ebx to 0
mov ecx, TCSETA					;Move the SET termios argument into ecx
lea edx, [struct_t]				;Calculate the effective address of the termios structure
int KERNEL						;Invoke the kernel
jo error						;If we overflowed our buffre, throw an exception
  validationRouter:
mov eax, [inputType]			;Move our input type back into eax to set up another switch
xor ecx, ecx					;Reset our counter
cmp eax, UINT32					;Validating UINT32?
je validateUINT32				;Yes? Jump to its validator
cmp eax, SINT32					;Validating SINT32?
je validateSINT32				;Yes? Jump to its validator
cmp eax, UINT16					;Validating UINT16?
je validateUINT16				;Yes? Jump to its validator
cmp eax, SINT16					;Validating SINT16?
je validateSINT16				;Yes? Jump to its validator
cmp eax, SBYTE					;Validating SByte?
je validateSByte				;Yes? Jump to its validator
cmp eax, UBYTE					;Validating UByte?
je validateUByte				;Yes? Jump to its validator
cmp eax, CHAR					;Validating Char?
je validateChar					;Yes? Jump to its validator
  validateUINT32:
xor eax, eax					;Clear eax
mov edx, activeBuckets			;Get the amount of buckets
xor ecx, ecx					;Reset the counter
vUINT32Loop:
	mov ebx, returnValue
	;cmp ebx, 48
	;jb error
	;cmp ebx, 57
	;ja error
	shl eax, 1
	sub ebx, 48
	add eax, ebx
	inc ecx:
	cmp ecx, edx
	jne vUINT32Loop
	mov eax, SYS_WRITE
	mov edx, 10
	mov ebx, 1
	int KERNEL
  validateSINT32:
  validateUINT16:
  validateSINT16:
  validateSByte:
  validateUByte:
  validateChar:
  exit:
popa
mov eax, 0
leave
ret
  error:
  ;	mov ecx, ebx
  ;	mov edx, 5
  ;	mov ebx, 1
  ;	mov eax, SYS_WRITE
  ;	int KERNEL
jmp exit
  ;mov ecx, ERRORTXT
  ;	mov edx, ERRORLEN
  ;	mov ebx, 1
  ;	mov eax, 4
  ;	int KERNEL
  ;	jmp exit

Linux Sucks

Time and time again, terminal freezes, work is lost, etc. Fuck Linux, seriously.

Prime number assembly solution A:

  ;
  ;~Zilent~ Team FoFF ex-member {Fight Only For Freedom}
  ;
  %include "asm_io.inc"
  segment .data
      ;We could sacrifice disk space for execution speed here.
      ;specialPrime2 db "2: Prime", 0
      ;specialPrime3 db "3: Prime", 0
      enterUpperRange db "Enter Upper Range: ", 0
      isPrime db ": Prime", 0
      isComposite db ": Composite", 0
  segment .bss
      upperRange resd 1
      currentDividend resd 1
  segment .text
      global _asm_main
  _asm_main:
      enter 0, 0                              ;OEP
      pusha                                   ;Push all registers onto stack
      mov eax, enterUpperRange                ;Move the entry string into eax
      call print_string                       ;Print it
      call read_int                           ;Get integer input value from user
      mov [upperRange], eax                   ;Tuck that value away at memory location denoted 'upperRange'
      mov eax, 2                              ;Move the special case value 2 into eax
      call print_int                          ;Print it
      mov eax, isPrime                        ;Move the prime text into eax
      call print_string                       ;Print it
      call print_nl                           ;Print newline
      mov eax, 3                              ;Move the special case value 3 into eax
      call print_int                          ;Print it
      mov eax, isPrime                        ;Move the prime text into eax
      call print_string                       ;Print it
      call print_nl                           ;Print newline - now the fun begins.
      mov dword [currentDividend], 4          ;Move '4' into the memory location denoted 'currentDivisor'
  outerLoop:
      mov eax, [currentDividend]              ;Which prime are we on?
      cmp eax, [upperRange]                   ;Are we equal to the limit?
      ja short finishedOuter                  ;Jump if current number is ABOVE the limit
      mov ebx, 2                              ;Move 2 into ebx, our iterator register
  innerLoop:
      mov eax, ebx                            ;Move ebx -> eax
      mul eax                                 ;eax *=  2
      cmp eax, [currentDividend]              ;Compare eax to the current `prime suspect` ha ha
      jae short finishedInner                 ;If eax * 2 is greater than or equal to our suspect, there's no reason to get the mod
                                              ;Look @ it this way, n/2 is the largest evenly divisible factor of n, other than n itself
      mov eax, [currentDividend]              ;Else, move the suspect into eax
      mov edx, 0                              ;Set up edx to contain the remainder
      div ebx                                 ;Divide eax by edx, storing the modulus value in edx
      cmp edx, 0                              ;Compare edx to 0
      je finishedInner                        ;If edx = 0, the number is composite
      inc ebx                                 ;Increment ebx (The # we are dividing the suspect by)
      jmp short innerLoop                     ;Head back up to the start of the loop
  finishedInner:
      je composite                            ;If edx = 0, then the number is composite
      mov eax, [currentDividend]              ;else, the number is prime, so we move it into eax to spit out
      call print_int                          ;Print `prime suspect`
      mov eax, isPrime                        ;Move the prime string into eax
      call print_string                       ;Print the prime string
      call print_nl                           ;Print a newline
      add dword[currentDividend], 1           ;Increment the `prime suspect` by one
      jmp outerLoop;                          ;Jump back to the outer loop to get the next `prime suspect`
  composite:
      mov eax, [currentDividend]              ;Move the `prime suspect` (caught being composite, that bastard) into eax
      call print_int                          ;Print `prime suspect`
      mov eax, isComposite                    ;Move the composite string into eax
      call print_string                       ;Print it
      call print_nl                           ;Print a new line character
      add dword[currentDividend], 1           ;Increment the `prime suspect` by one
      jmp outerLoop
  finishedOuter:
      popa                                    ;Pop all previously pushed register values from the stack
      mov eax, 0                              ;Move the exit code into eax
      leave                                   ;Clean up the stack
      ret                                     ;Return to the calling function

Theory on Reversing Jumpers

      ====== JNBE : JA ======
      JNBE:
      if(!x <= y) { //x > y??? Why JNBE, sounds pointless
          //Do this
      } else {
          //Do that
      }
      JA:
      if(x > y) {
          //Do that
      } else {
          //Do this
      }
      

Project : Text Based Adventure Game @ssembly

  //C++
  class FlyingGame {
private:
	BufferToggle toggle;
	int difficulty;
	bool gameGrid[10][10]
	static const char Player = '^';
	static const char object = '*';
	static const char voidObj = ' ';
	bool alive;
	int playerPosition;
public:
        FlyingGame() {
		playerPosition = 5;
		srand(time(0));
		alive = true;
		for(int x=0; x<10; x++) {
			for(int y=0; y<10; y++) {
				gameGrid[x][y] = false;
			}
		}
		cout << "Which Difficulty Level? (0-5): ";
		cin >> difficulty;
		difficulty++;
		while(!cin || difficulty < 0 || difficulty > 5) {
				cout << "Invalid Entry...\n";
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cout << "Which Difficulty Level? (0-5): ";
				cin >> difficulty;
		}
		toggle.off();
	}
	FlyingGame() {
			toggle.on();
	}
	void addNewRow()  {
			for(int x = 0; x<10; x++) {
					gameGrid[x][0] = false;
			}
			int random;
			for(int spawn = 0; spawn < difficulty; spawn++) {
				random = (rand()%10)+1;
				if(gameGrid[random][0] == true) {
						spawn--;
				} else {
						gameGrid[random][0] = true;
				}
			}
	}
void advanceRows() {
	for(int y = 9; y > 0; y--) {
		for(int x = 0; x<10; x++) {
			gameGrid[x][y] = gameGrid[x][y-1];
		}
	}
	addNewRow();
}
void paintAdvancedRows() {
	system("clear");
	for(int y=0; y<9; y++) {
		for(int x=0; x<10; x++) {
			if(gameGrid[x][y] == true) {
					cout << object;
			} else {
					cout << voidObj;
			}
		}
		cout << '\n';
	}
	for(int x = 0; x<10; x++) {
		if(gameGrid[x][9] == true && x != playerPosition) {
			cout << object;
		}
		if(gameGrid[x][9] == false && x != playerPosition) {
			cout << voidObj;
		}
		if(x == playerPosition) {
			cout << player;
		}
	}
	cout << '\n';
}
	void movePlayerLeft()  {
		if(playerPosition > 0) {
			playerPosition--;
		}
	}
void movePlayerRight() {
	if(playerPosition < 9) {
		playerPosition++;
	}
}
bool getIsDead() {
	bool isDead;
	if(gameGrid[playerPosition][9] == true) {
		isDead = true;
	} else {
		isDead = false;
	}
	return isDead;
}
  };
  FlyingGame FG = FlyingGame();
  void *mainThread(void* damnNotEncapsulated) {
char ch;
while(1) {
	if(cin.get(ch)) {
		if(ch == 'D') {
			FG.movePlayerLeft();
		}
		if(ch == 'C') {
			FG.movePlayerRight();
		}
	}
}
  }
  int main() {
pthread_t gameThread;
pthread_attr_t attributes;
pthread_attr_init(&attributes);
pthread_create(&gameThread, &attributes, mainThread, NULL);
while(!FG.getIsDead()) {
	usleep(500000);
	FG.advanceRows();
	FG.paintAdvancedRows();
}
return 0;
  }
     ===ASM===
     Coming soon, maybe?  If I feel like it.
     
user/lleber/start.txt · Last modified: 2011/08/31 23:01 by lleber