Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help with if statement

probleme with if stat ment


This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 05 April 2015 - 08:34 AM

this is my code :

extern _printf

section .data 
    A : db 10
    B : db 20
    tex:db "A > B" ,10,0
    tax  : db "B>A" ,10,0
    de : db "%s",0
    
section .text
global _main

_main:
    push ebp
    mov ebp , esp
    xor eax , eax 
    xor ebx , ebx
    mov eax , A
    mov ebx , B
    cmp eax , ebx 
    jge AB
    add esp , 8
    push tex
    push de
    call _printf
    sub esp ,8 
    jmp endd
    
AB:
    add esp , 8
    push tax
    push de
    call _printf
    sub esp , 8
    
endd:
    mov esp , ebp 
    pop ebp

 

----

Probleme : it should the output to be like this : B > A

I've tried to changed the value of A and B but still same probleme So I changed "Jng" to "Jge" it changed the output to A > B but when I change Th value of B to be bigger thn A still same output   


Edited by dargueta, 07 April 2015 - 05:03 PM.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 07 April 2015 - 05:04 PM

You're setting EAX and EBX to the addresses of A and B, not the actual values there. Change this:

mov eax , A
mov ebx , B

to this:

mov eax , [A]
mov ebx , [B]

and it should work.

 

 

Edit: Side note - for variable data, use the .bss segment, as .data is usually read-only. Thus, your declaration should look like:

section .bss
    A : resb 10
    B : resb 20

section .data 
    tex: db "A > B" ,10,0
    tax: db "B>A" ,10,0
    de : db "%s",0

Note that in the .bss segment we use resb instead of db.


Edited by dargueta, 07 April 2015 - 05:08 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 08 April 2015 - 06:47 AM

first of 

 

You're setting EAX and EBX to the addresses of A and B, not the actual values there. Change this:

mov eax , A
mov ebx , B

to this:

mov eax , [A]
mov ebx , [B]

and it should work.

 

 

Edit: Side note - for variable data, use the .b


Edited by zika, 08 April 2015 - 06:53 AM.


#4 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 08 April 2015 - 07:21 AM

thanx for ur reply but still same probleme  :(



#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 April 2015 - 08:18 AM

Your branch condition is also wrong. cmp works exactly like sub, so a jge after cmp eax, ebx is jumping if eax >= ebx. Change jge AB to jb AB and it'll work.


sudo rm -rf / && echo $'Sanitize your inputs!'


#6 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 08 April 2015 - 09:52 AM

emmm it change the resault but when I'v changed B  value to be less Then A value still print same as B bigger then A

I think jge it's mean greater then or equal 



#7 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 April 2015 - 10:03 AM

I think jge it's mean greater then or equal 

 

Yes, I know. But in the Intel Software Developer's Manual, it says:

 

The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction.

 

So cmp does eax - ebx, meaning if we want to jump when eax >= ebx (i.e. the result is >= 0) then we must do jge. However, if you look at your code in AB, you print out tax, which is "B>A". So either your jump condition or your string is wrong.


Edited by dargueta, 08 April 2015 - 10:03 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#8 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 08 April 2015 - 10:23 AM

so lets assume we have to values A and B and lets say A = 20 and B = 10

when make cmp between two values

mov eax,[A]
mov ebx ,[B]
cmp eax , ebx ; so here ebx-eax which mean 20 - 10 = 10 
;here the resault A is bigger then B 
jge caseBA

printf("\nA bigger then B"); case 1
caseBA:
printf("\nB bigger then A")
exit: 

the output will be true but when change B to be bigger then A the output it will be wrong but when I change jge to jng the output will be true 

but agin when I change value of B to be less then A the output it will be wrong


Edited by zika, 08 April 2015 - 10:26 AM.


#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 April 2015 - 10:46 AM

No, in line 3 you've got it backwards. eax - ebx. You also contradict yourself: In your comment you say "A is bigger than B" but you print out "B bigger than A".


Edited by dargueta, 08 April 2015 - 10:48 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 08 April 2015 - 11:00 AM

sorry for that mistake ; 

what I mean is B bigger then A 

lets make it easier how can write this in  asm

 int A =20;
 int B=10;
if(A>B)
{
}
else {

}


Edited by zika, 08 April 2015 - 11:09 AM.


#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 08 April 2015 - 12:05 PM

mov     eax, [A]
mov     ebx, [B]

cmp     eax, ebx
jle     a_less_b
; Do stuff here for A > B
jmp     end

a_less_b:
    ; Do stuff here for A <= B
    
end:
    ; Resume execution

sudo rm -rf / && echo $'Sanitize your inputs!'


#12 zika

zika

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 09 April 2015 - 08:54 AM

still same probleme look at this picture

same resault 

 

583zlYm.jpg

 

and my code  please try to compile it and see the output

 

case 1: 

extern _printf 
Section .data
A : db 2
B : db 10
msgA : db "A bigger thn B",10,0
msgB : db "B bigger thn A",10,0
typ : db "%s",0
Section .bss
Section .text
global _main
_main:
push ebp
mov ebp,esp
xor eax , eax 
xor ebx , ebx 
mov eax , [A]
mov ebx , [B]
cmp eax , ebx
jle a_less_b
 push msgA
 push typ 
 call _printf
 add esp , 8 
 jmp exit 
a_less_b:
push msgB
 push typ
 call _printf
add esp , 8
 exit :
mov esp , ebp
pop ebp
ret

case 2:

extern _printf 
Section .data
A : db 10 
B : db 2
msgA : db "A bigger thn B",10,0
msgB : db "B bigger thn A",10,0
typ : db "%s",0
Section .bss
Section .text
global _main
_main:
push ebp
mov ebp,esp
xor eax , eax 
xor ebx , ebx 
mov eax , [A]
mov ebx , [B]
cmp eax , ebx
jle a_less_b
 push msgA
 push typ 
 call _printf
 add esp , 8 
 jmp exit 
a_less_b:
push msgB
 push typ
 call _printf
add esp , 8
 exit :
mov esp , ebp
pop ebp
ret

-----

I'm using nasm and gcc as linker and my system is win7 64 bit