NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: maplesirop on February 23, 2013, 09:22:05 PM

Title: How do you do a embedded if in assembly?
Post by: maplesirop on February 23, 2013, 09:22:05 PM
Code: [Select]
(...)
cmp ecx, 48
jl warn
cmp ecx, 57
jg warn
inc ecx




warn: cmp ecx, 97
jle stop
cmp ecx, 122
jge stop
mov eax, 4
mov ebx, 1
mov ecx, alert
mov edx, len
int 0x80

warn: (...)




would this work?
Title: Re: How do you do a embedded if in assembly?
Post by: Gerhard on February 23, 2013, 09:35:00 PM
Hi maplesirop,

would this work?

probably not, because you've the same label (warn) two times in your source.

Gerhard
Title: Re: How do you do a embedded if in assembly?
Post by: maplesirop on February 24, 2013, 12:54:20 AM
thanks, would it work otherwise?
Title: Re: How do you do a embedded if in assembly?
Post by: Gerhard on February 24, 2013, 01:10:13 AM
Hi maplesirop,

thanks, would it work otherwise?

that's not so clear. Where is the label stop? What would you like to do? Do you need only the if-tree or the if-else-trees? In any case, that ressource could be of interst for you: http://www.drpaulcarter.com/pcasm/ (http://www.drpaulcarter.com/pcasm/). I hope that helps.

Gerhard
Title: Re: How do you do a embedded if in assembly?
Post by: maplesirop on February 24, 2013, 02:08:57 AM
Code: [Select]
I need to embbed several if statements

if (x=1)
{
x++
}
else
{
if (x=2)
{
x=0;
}
else
{
if(x=0)
{
x++;
}
}
}
Title: Re: How do you do a embedded if in assembly?
Post by: Gerhard on February 24, 2013, 02:53:19 AM
Hi maplesirop,

that looks like a case select:

Code: [Select]
  case 0:
    x++
  case 1:
    x++
  case 2:
    x=0

Is that your goal?

Gerhard
Title: Re: How do you do a embedded if in assembly?
Post by: maplesirop on February 24, 2013, 02:56:43 AM
Hi maplesirop,

that looks like a case select:

Code: [Select]
  case 0:
    x++
  case 1:
    x++
  case 2:
    x=0

Is that your goal?

Gerhard

yes

can you help me with my segmentation fault problem?
Title: Re: How do you do a embedded if in assembly?
Post by: Mathi on February 24, 2013, 12:23:36 PM
Code: [Select]
if (x=1)
{
x++
}
else
{
if (x=2)
{
x=0;
}
else
{
if(x=0)
{
x++;
}
}
}


can be translated to.

Code: [Select]
cmp ecx,1
jne elselabel1
inc ecx
jmp endiflabel1
elselabel1:
  cmp ecx, 2
jne elselabel2
mov ecx,2
jmp endiflabel2
elselabel2:
cmp ecx, 0
jne endiflabel3     ;;jump to endif directly
inc ecx
endiflabel3:
endiflabel2:
endiflabel1:

I would suggest to follow a label naming convention like this when you code a nested if-else constructs.

Try it and let us know.
Title: Re: How do you do a embedded if in assembly?
Post by: maplesirop on February 25, 2013, 04:40:53 AM
thanks