Author Topic: "Jump if Equal" doesn't work  (Read 347 times)

Offline MIchael

  • Jr. Member
  • *
  • Posts: 10
"Jump if Equal" doesn't work
« on: May 29, 2018, 04:14:44 PM »
I really can't understand why my code doesn't work correctly:

Code: [Select]
section .data

ok: db "Count of args isn't 1" , 10, 13, 0
err: db "Count of args is 1" , 10, 13, 0

section .text
    global _start
_start:

mov rax, [rsp]
cmp rax, 2             ;if ARGC = 1
jne ..@7.ifnot
mov rcx, ok
mov rax, 1
mov rdi, 1
mov rdx, 1

..@10.while:                ;print 'Count of args is 1\r\n' into console
    cmp [rcx], dword 0
    je ..@10.endwhile      ;This line
    mov rsi, rcx
    push rcx
    syscall
    pop rcx
    inc rcx
    jmp ..@10.while
..@10.endwhile:

jmp ..@7.ifend

..@7.ifnot:           ;if ARGC !== 1
    mov rcx, err
    mov rax, 1
    mov rdi, 1
    mov rdx, 1

..@15.while:           ;print 'Count of args isn't 1\r\n'
    cmp [rcx], dword 0
    je ..@15.endwhile
    mov rsi, rcx
    push rcx
    syscall
    pop rcx
    inc rcx
    jmp ..@15.while
..@15.endwhile:

..@7.ifend:            ;and exit
    mov rax, 60
    mov rdi, 0
    syscall


When I run `./test` it output "Count of args isn't 1" (expected behavior). But if I run `./test abcd` it print

Code: [Select]
Count of args is 1
Count of args isn't 1

When the next byte at the address RCX is 0 and program reaches "je ..@15.endwhile" line, it simply does not jump to "..@15.endwhile."

Why?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2329
  • Country: us
Re: "Jump if Equal" doesn't work
« Reply #1 on: May 29, 2018, 08:39:25 PM »
I'll try to get into this... later...
For now, let me try a wild-assemed guess...
Code: [Select]
..@15.while:           ;print 'Count of args isn't 1\r\n'
    cmp [rcx], dword 0
    je ..@15.endwhile
As you say, the next byte is zero. But you check for dword. Try changing that to byte.

Best,
Frank


Offline MIchael

  • Jr. Member
  • *
  • Posts: 10
Re: "Jump if Equal" doesn't work
« Reply #2 on: May 30, 2018, 12:49:17 PM »
Thank you!