Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

TASM, validate user input 5digit, how?

user input validate

  • Please log in to reply
14 replies to this topic

#13 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 06 February 2011 - 12:14 AM

i give up..im just a slow learner..thank you for time and effort regarding this matter..i wonder if would you like to post the code and i will try to figure out my discrepancies..im not an expert..nobody is perfect..thats why im here , and there is testing and debug portion of the procedures and there is a collaboration between other people around this forum to discuss the best way how to right a good codes..share the codes and contribute the effort, study the codes and apply what we have learned here in the future..thank a lot RhetoricalRuvim
  • 0

#14 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 06 February 2011 - 12:16 AM

this is our case study..this would be affect our overall midterm examination.. this is due today..i hope you understand...im very eager to learn more some codes...
  • 0

#15 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 06 February 2011 - 07:22 AM

You don't have to give up.


Some instructions:

MUL
;; Let's say we want to perform "ax= bl * cl" 
mov al, bl  ;; The MUL instruction multiplies al or ax by its operand, 
;; so we might want to put bl into al 
;; (since we're multiplying by an 8-bit integer, MUL uses al). 
mul cl       ;; Multiply whatever is in al by cl 
;; And that's it, ax should now be bl * cl

DIV
;; Let's try something like "ax= bx / cx" 
mov ax, bx    ;; The dividend. 
xor dx, dx      ;; The higher part of the dividend; DIV, when dividing by a 16-bit register, 
;; uses DX:AX as the dividend, so we should clear DX first. 
div cx            ;; And now the actual divide instruction. 
;; Now, after the DIV instruction, ax is the quotient (bx / cx)
;;  and dx is the remainder or the modulus (bx % cx).

ADD
;; Let's say "ax= bx + cx" 
mov ax, bx    ;; The number to add to. 
add ax, cx     ;; Add cx to ax. 
;; And that's it.
;; Let's look at another example that uses a memory operand: "ax= ax + number" 
add ax, [number]           ;; We could also use something like 'word [number]' or 'word ptr [number]', 
;; or whatever the syntax is for TASM. 
;; Now ax is equal to ax plus number.

SUB
;; Let's try "bx= cx - bx" (while multiply and divide instructions require ax, add and subtract are more flexible). 
push cx     ;; Save cx 
sub cx, bx  ;; Perform the subtraction. 
mov bx, cx ;; Move the new value to bx, where it belongs. 
pop cx       ;; Restore cx

CMP
;; You probably know this instruction, already. 
;; Basically what it does is check if operand1 minus operand2 is equal to, greater than, or less than zero, 
;; without storing the result of the subtraction in operand1; you can also use the SUB instruction, 
;; which would do the same thing except that it would actually store the result. 
cmp ax, 65  ;; Compare ax to 65. 
jg .....  ;; If greater, go to .....  
jl .....  ;; If less, go to .....  
jge .....  ;; If greater or equal, go to .....  
jle .....  ;; If less or equal, go to .....  
jb .....  ;; If below, go to .....  (I'm not too sure about what "below" means) 
ja .....  ;; If above, go to .....  
jng .....  ;; If not greater, go to .....  
jnl .....  ;; If not less, go to .....  
jnge .....  ;; If not greater or equal, go to .....  
jnle .....  ;; If not less or equal, go to .....  
jmp .....  ;; Else, go to .....  
;; By the way, JE and JZ assemble to the same opcode (or machine code), 
;; it's just they can be called differently.

I think you know the MOV, CALL, RET, PUSH, POP, and maybe some other instructions. If you don't you can either ask or search for "<instruction name> intel instruction" on google or some other search engine.

And also, there are PUSHA and POPA instructions that push all the general-purpose registers and pop all the general-purpose registers in some order (I forgot the order, but if you look it up then you can find the order).

Well, anyway, I coded the program using NASM.

The code might look complicated, but it's actually very simple; it just follows the steps I posted earlier.

puts()
puts: 
	ret 2  ;; For this, we can just return, since this is just an example. You don't need to return this early in your code. 
	push bp 
	mov bp, sp 
	pusha 
	
	mov ax, [bp+4] 
	mov bx, ax 
	
	mov ah, 10b 
	
	.lp1: 
		mov al, [bx] 
		cmp al, 0 
		jz .lp1s 
		
		mov dl, al 
		int 0x21 
		
		inc bx 
		jmp .lp1 
	.lp1s: 
	
	popa 
	mov sp, bp 
	pop bp 
	ret 2 
;; .....

gets()
gets: 
	ret 2  ;; For this, we can just return, since this is just an example. You don't need to return this early in your code. 
	push bp 
	mov bp, sp 
	pusha 
	
	mov ax, [bp+4] 
	mov bx, ax 
	mov dx, ax 
	
	mov ah, 1 
	
	.lp1: 
		int 0x21 
		cmp al, 13 
		jz .lp1s 
		cmp al, 10          ;; Not sure how that might happen (DOS usually inputs 13, when the return key is pressed). 
		jz .lp1s 
		
		mov [bx], al 
		inc bx 
		
		cmp al, 8           ;; Check if backspace. 
		jnz .lp1 
		
		sub bx, 2 
		
		cmp bx, dx 
		jnl .lp1 
		mov bx, dx 
		
		jmp .lp1 
	.lp1s: 
	mov byte [bx], 0 
	
	popa 
	mov sp, bp 
	pop bp 
	ret 2 
;; .....

strcmp()
strcmp: 
	ret 4  ;; For this, we can just return, since this is just an example. You don't need to return this early in your code. 
	push bp 
	mov bp, sp 
	sub sp, 2 
	pusha 
	
	mov ax, [bp+4] 
	mov bx, ax 
	mov ax, [bp+6] 
	mov dx, ax 
	
	mov al, 1 
	
	.lp1: 
		cmp ah, 0 
		jz .lp1s 
		
		mov al, [bx] 
		xchg bx, dx 
		mov ah, [bx] 
		xchg bx, dx 
		
		inc bx 
		inc dx 
		
		sub al, ah 
		jz .lp1 
		
		and ah, 0 
		
		mov word [bp-2], ax 
		jmp .finish 
	.lp1s: 
	xor ax, ax 
	mov word [bp-2], ax 
	
	jmp .finish 
	
	.finish: 
	
	popa 
	
	mov ax, [bp-2] 
	
	mov sp, bp 
	pop bp 
	ret 4 
;; .....

Although if you want to use functions like this then you probably should make your own versions. These are just examples of how you could code them.

Then you call the functions somehow like this:

.data
msg db "Hello, how are you? ", 13, 10, 0 ;; msg label, db directive, the message, new line (13, 10), and a null character to indicate the end of the string.

.code

push word ptr offset msg ;; or however this is in TASM
call puts


And gets() is similar:

;; I don't know about TASM, but NASM is like this:
.bss
string1 resb 256 ;; Reserve 512 bytes for the string (or you could reserve less).
;; And this is how it is in MASM:
.data?
string1 DB 256 dup (?)

.code

push word ptr offset string1
call gets


And the strcmp() function:

push word ptr offset string2
push word ptr offset string1
call strcmp

;; and if the strings are equal then ax would be 0.
  • 0





Also tagged with one or more of these keywords: user input, validate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download