Author Topic: Problem with control flow(cmp and jne)  (Read 16144 times)

Offline Teol

  • Jr. Member
  • *
  • Posts: 12
Problem with control flow(cmp and jne)
« on: November 11, 2016, 10:39:51 AM »
So my problem is that for some reason that i cant understand the code remains in a loop looping the convertInput even though i thougth it should break from the loop
when ecx reaches 1. Can someone help me understand why its looping and and help to fix this?
Thanks already in advance!
Code: [Select]
handleInput:
xor ecx, ecx   ;Zero, ecx for counter to use.
xor eax, eax   ;Zero the eax.
mov [inputValue], byte 0   ;Reset the inputValue.

convertInput:
mov al, byte [input + ecx]       
sub eax, '0'  
push eax
cmp ecx, 1
inc ecx
call printDebug
jne convertInput   ;if ZF=0 jump

        ... more code here, why is this not reached???

Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: Problem with control flow(cmp and jne)
« Reply #1 on: November 11, 2016, 11:47:41 AM »
The way in which your instructions are ordered is wierd.

try something like:

Code: [Select]
mov al, byte [input + ecx]
sub eax, '0'
push eax
call printDebug
inc ecx
cmp ecx, 1
jne convertInput

Or give me the full code so I can run it in a debugger.

NOTE: you shouldn't do to much stuff between the "cmp ecx, 1" and "jne convertInput" instructions. If the flags get altered between these two instructions, it will mess up your program flow. Calling a subroutine (call printDebug) between them is definitely a "no no".
« Last Edit: November 11, 2016, 11:56:07 AM by soulvomit »

Offline Teol

  • Jr. Member
  • *
  • Posts: 12
Re: Problem with control flow(cmp and jne)
« Reply #2 on: November 11, 2016, 12:35:09 PM »
Thank you for the advice, how can i debug it in linux say in ubuntu what software etc i need?

Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: Problem with control flow(cmp and jne)
« Reply #3 on: November 11, 2016, 12:38:36 PM »
Thank you for the advice, how can i debug it in linux say in ubuntu what software etc i need?
On linux GDB (gnu debugger) is probably the best choice. Personally as a windows user I hate it, and use x32dbg/x64dbg. Unless I'm doing remote debugging, where GDB is superior.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Problem with control flow(cmp and jne)
« Reply #4 on: November 11, 2016, 01:48:10 PM »
Ahhh... the forum is pissin' on me!

http://home.myfairpoint.net/fbkotler/debug-0.0.21.tgz

It's a debugger by Terry Loveall. Like DOS debug. Learn to use gdb. Just because I'm too lazy to doesn't mean you should be. I've got a couple debuggers by Jeff Owens, too. Maybe some other time.

Best,
Frank


Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: Problem with control flow(cmp and jne)
« Reply #5 on: November 11, 2016, 02:17:54 PM »
The GDB command line is hellish to work with, imo. Should be a fair few good graphical front ends for GDB. Frank will know which :)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Problem with control flow(cmp and jne)
« Reply #6 on: November 11, 2016, 02:26:25 PM »
Actually... I've neber had  much luck with front ends for gdb either. I can't even type today.

Later,
Frank


Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: Problem with control flow(cmp and jne)
« Reply #7 on: November 11, 2016, 05:09:24 PM »
Here are some comments on your code:

Code: [Select]
handleInput:
xor ecx, ecx   ;ecx=0
xor eax, eax   ;eax=0
mov [inputValue], byte 0   ;inputValue=0 - It is hard to discern what inputValue is from what you provided.

convertInput:
mov al, byte [input + ecx]    ;move "the value" at input+ecx into the lowest byte of eax - What is this value?
sub eax, '0' ;subtract 48 from "the value"
push eax ;push "the value" to the stack - Is this an arg for printDebug?
cmp ecx, 1              ;compare ecx to 1 - Will set the zero flag state
inc ecx                 ;ecx=ecx+1 - This will cause the zero flag state of "cmp ecx, 1" to be overwritten
call printDebug         ;call printDebug - Since you didnt provide the code for this, we don't know what it does to the zero flag. But there is a good possibility that it overwrites the overwritten zero flag state.
jne convertInput ;jump to convertInput if zero flag=0 - Zero flag will always be 0, since "inc ecx" never results in ecx+1=0. This is probably why you get a infinite loop.
Sorry about the formatting issues, the forum did a hack job on my tabulation.
« Last Edit: November 11, 2016, 05:56:40 PM by soulvomit »

Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: Problem with control flow(cmp and jne)
« Reply #8 on: November 11, 2016, 05:33:36 PM »
Double post.
« Last Edit: November 11, 2016, 05:41:32 PM by soulvomit »

Offline Teol

  • Jr. Member
  • *
  • Posts: 12
Re: Problem with control flow(cmp and jne)
« Reply #9 on: November 20, 2016, 09:05:03 PM »
I have been learning to use the gdb. The inc command changes the ZF flag so this explains what was happening. :-)
It is documented also for example on this site: http://c9x.me/x86/html/file_module_x86_id_140.html