Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x86 NASM Assembly - Problems with Input
- %include "system.inc"
- section .data
- greet: db 'Hello!', 0Ah, 'Please enter a word or character:', 0Ah
- greetL: equ $-greet ;length of string
- inform: db 'I will now repeat this until you type it back to me.', 0Ah
- informL: equ $-inform
- finish: db 'Good bye!', 0Ah
- finishL: equ $-finish
- newline: db 0Ah
- newlineL: equ $-newline
- section .bss
- input: resb 40 ;first input buffer
- check: resb 40 ;second input buffer
- section .text
- global _start
- _start:
- greeting:
- mov eax, 4
- mov ebx, 1
- mov ecx, greet
- mov edx, greetL %include "system.inc"
- section .data
- greet: db 'Hello!', 0Ah, 'Please enter a word or character:', 0Ah
- greetL: equ $-greet ;length of string
- inform: db 'I will now repeat this until you type it back to me.', 0Ah
- informL: equ $-inform
- finish: db 'Good bye!', 0Ah
- finishL: equ $-finish
- newline: db 0Ah
- newlineL: db $-newline
- section .bss
- input: resb 40 ;first input buffer
- check: resb 40 ;second input buffer
- section .text
- global _start
- _start:
- greeting:
- mov eax, 4
- mov ebx, 1
- mov ecx, greet
- mov edx, greetL
- sys.write
- getword:
- mov eax, 3
- mov ebx, 0
- mov ecx, input
- mov edx, 40
- sys.read
- sub eax, 1 ;remove the newline
- push eax ;store length for later
- instruct:
- mov eax, 4
- mov ebx, 1
- mov ecx, inform
- mov edx, informL
- sys.write
- pop edx ;pop length into edx
- mov ecx, edx ;copy into ecx
- push ecx ;store ecx again (needed multiple times)
- mov eax, 4
- mov ebx, 1
- mov ecx, input
- sys.write
- mov eax, 4 ;print newline
- mov ebx, 1
- mov ecx, newline
- mov edx, newlineL
- sys.write
- mov eax, 3 ;get the user's word
- mov ebx, 0
- mov ecx, check
- mov edx, 40
- sys.read
- xor eax, eax
- checker:
- mov ebx, check
- mov ecx, input
- cmp ebx, ecx ;see if input was the same as before
- jne loop ;if not the same go to input again
- je done ;else go to the end
- pop edx
- mov ecx, edx
- push ecx
- mov eax, 4
- mov ebx, 1
- mov ecx, check
- sys.write ;repeat the word
- mov eax, 4
- mov ebx, 1
- mov ecx, newline
- mov edx, newlineL
- sys.write
- loop:
- mov eax, 3 ;replace new input with old
- mov ebx, 0
- mov ecx, check
- mov edx, 40
- sys.read
- jmp checker
- done:
- mov eax, 1
- mov ebx, 0
- sys.exit
- sys.write
- getword:
- mov eax, 3
- mov ebx, 0
- mov ecx, input
- mov edx, 40
- sys.read
- Hello!
- Please enter a word or character:
- Nick
- I will now repeat this until you type it back to me.
- Nick
- (I input) Magerko
- (I get) M
- (I input)Nick
- (I get)
- (I input)Nick
- (I get)
- checker:
- mov ebx, check ; Moves the *address* of check into ebx
- mov ecx, input ; Similarly for input
- cmp ebx, ecx ; Checks if the addresses are the same (they never are)
- mov eax, [input_len]
- cmp eax, [check_len]
- jne loop ; Strings of different length, do loop again
- mov ebx, check
- mov ecx, input
- .checkloop:
- mov dl, [ebx] ; Read a character from check
- cmp dl, [ecx] ; Equal to the character from input?
- jne loop ; Nope, jump to `loop`
- inc ebx ; Move ebx to point at next character in check
- inc ecx ; and ecx to next character in input
- dec eax ; one less character to check
- jnz .checkloop ; done?
- ; the strings are equal if we reach this point in the code
- jmp done
- jne loop ;if not the same go to input again
- je done ;else go to the end
- pop edx ; Will never be reached!
Add Comment
Please, Sign In to add comment