NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: 0xDEADBEEF on March 09, 2017, 01:26:31 PM

Title: Comparison of linux terminal input
Post by: 0xDEADBEEF on March 09, 2017, 01:26:31 PM
Hello

i try to make a little game where the player gets controllerd by w a s and d into the terminal. Its just to learn NASM :)
I have a problem with the player input comparison and jumping to the condition...

i try to compare here the value of the input with a static 'w' value to check if the user entered w. For some reason it doesnt get triggered and i implemented a debug message to show what was entered and it shows it was 'w'.

Quote
Input is not "w" but its: w

This confuses me really hard since i cant seem to find what is wrong...

Thanks a lot for your time! :)

Code: [Select]
section .data
    msg: db 'Input is "w"', 10
    msg_L: equ $-msg
    msg2: db 'Input is not "w" but its: '
    msg2_L: equ $-msg
   
section .bss
    playerInput: resb 1
   
section .text
  global _start:
 
_start:
    begin:
   
    call _getPlayerInput
    call _moveInput
    jmp begin

   
_getPlayerInput:
    mov ecx, playerInput ; load input into playerInput
    mov edx, 2
    call __sys_read

    ret
   
_moveInput:
    ; comparison starts here
    mov eax, [playerInput] ; move value of playerInput into eax
    mov ebx, 'w'           ; load 'w' to compare
    cmp eax, ebx           ; compare playerInput with 'w'
    je moveInputW          ; if input is w then go for moveInputW
    jne moveInputE         ; else go for moveInputE
   
    moveInputW:
    mov ecx, msg           ; print msg that input is w
    mov edx, msg_L
    call __sys_print
    ret
   
    moveInputE:
    mov ecx, msg2          ; print msg that input IS NOT w
    mov edx, msg2_L
    call __sys_print
   
    mov ecx, playerInput   ; and print what it is instead
    mov edx, 1
    call __sys_print
    call __sys_exit
   
   
; -------------------------------------------------
; System calls
; -------------------------------------------------
__sys_read:
    mov eax, 3
    mov ebx, 1
    int 80h
    ret
   
__sys_print:
    mov eax, 4
    mov ebx, 1
    int 80h
    ret
   
__sys_exit:
    mov eax, 1
    mov ebx, 0
    int 80h
Title: Re: Comparison of linux terminal input
Post by: Frank Kotler on March 10, 2017, 05:14:35 AM
Code: [Select]
_moveInput:
    ; comparison starts here
    mov eax, [playerInput] ; move value of playerInput into eax

You're moving four bytes into eax - your one byte of input, plus the linefeed (sys_read always gets this), plus a couple of "garbage" bytes. Naturally it doesn't compare equal to "w". Use al instead of eax, and similar one byte registers for the comparison(s).

Best,
Frank

Title: Re: Comparison of linux terminal input
Post by: 0xDEADBEEF on March 10, 2017, 05:40:13 AM
Oh wow, this is so obvious...
For some reason, it seemed to work perfectly fine with the 32bit registers, and then all of a sudden it stopped working, I could have tought that this is the issue...

Thanks a lot Frank!
You truly deserve your forum-title! Really appreciate that you take some of your valuable time to help newbies like me. I hope that some day, i might be useful for this community too :) (The NASM train got me!)

Have a great day!
Title: Re: Comparison of linux terminal input
Post by: Stefnet on March 10, 2017, 04:09:16 PM
Hey, I'm also beginner and I couldn't say what was wrong when I was looking at your code... But yeah, I still have much more left to learn :D

Thanks for fixing that Frank! My understanding of the code grows stronger :D