4 Replies - 945 Views - Last Post: 24 October 2015 - 12:23 AM

#1 SEnergy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 15

Help with comparing 2 numbers

Posted 21 October 2015 - 09:19 AM

Hello,

I'm pretty new to assembly and I need to use it withing the school project, what I need to do is an assembly code that will move a number to eax and with another function it will compare input with the saved variable, however I'm doing this in C using asm function, but assembly part is the one that needs fixing, the output is "smaller" all the time, even if I change the numbers

#include <stdio.h>

void setFlag(), compare(), smaller(), larger();
int getFlag(), getCurr();

int main()
{
	setFlag();
	compare();
	return 0;
}

void setFlag()
{
	//sets eax to 9
	asm("movl %0, %%eax;"
		:: "r" (getFlag())
		: "%eax");
}

void compare()
{
	//compares 3 with eax (should be 9)
	asm("cmpl %0, %%eax;"
		"jg lrg;"
		"call smaller;"
		"jmp out;"
		"lrg:"
		"call larger;"
		"out:"
		:: "r" (getCurr())
		: "%eax");	
}

void smaller() { printf("smaller"); }

void larger() { printf("larger"); }

int getFlag() { return 9; }

int getCurr() { return 3; }


I'm not sure how this forum works, but I can't find edit button, but I'm using AT&T syntax, compiling with gcc -std=gnu99 on linux

edit2: ok, I tried replacing
jg lrg;
with
jl lrg;
to see if that actually works, but I still am getting "smaller" output, like, why? I really have no idea what I'm doing, never touched assembly before today

Is This A Good Question/Topic? 0
  • +

Replies To: Help with comparing 2 numbers

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6824
  • View blog
  • Posts: 31,469
  • Joined: 10-May 07

Re: Help with comparing 2 numbers

Posted 21 October 2015 - 09:23 AM

I'm not familiar with AT&T syntax, so I can't really be of much help. But if this were my project, I would focus on learning assembly outside of C, & then once I understood how it worked, I would introduce it to C. This removes the compiler from the possibility of causing a false-negative.

Looking over the the logic, & again just trying to remove C as the issue, I would print out the register before comparing it. Does the register carry the value between the two functions? When you enter compare(), your comment says it's comparing 9 & 3, but is it really?

Also, just to note, we don't allow editing by new members due to abuse.
Was This Post Helpful? 0
  • +
  • -

#3 SEnergy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 15

Re: Help with comparing 2 numbers

Posted 21 October 2015 - 09:28 AM

View Postno2pencil, on 21 October 2015 - 09:23 AM, said:

I'm not familiar with AT&T syntax, so I can't really be of much help. But if this were my project, I would focus on learning assembly outside of C, & then once I understood how it worked, I would introduce it to C. This removes the compiler from the possibility of causing a false-negative.

Looking over the the logic, & again just trying to remove C as the issue, I would print out the register before comparing it. Does the register carry the value between the two functions? When you enter compare(), your comment says it's comparing 9 & 3, but is it really?

Also, just to note, we don't allow editing by new members due to abuse.


there really is no time to learn assembly, I'll be using it only for this project so I need only few lines without losing hair over assemblers and stuff

AT&T syntax is pretty much the same, but destination and source are switched, at least from what I did read
Was This Post Helpful? 0
  • +
  • -

#4 SEnergy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-October 15

Re: Help with comparing 2 numbers

Posted 21 October 2015 - 09:56 AM

tried to play with this a little bit, the output is "9 0 equal", something's really, really wrong, which is weird, because inEax function does basically the same thing as second part of setFlag, so the variable is removed from the memory somehow at the end of the setFlag function??

#include <stdio.h>

void setFlag(), compare(), smaller(), larger(), equal(), inEax();
int getFlag(), getCurr();

int main()
{
	setFlag();
	inEax();
	compare();
	return 0;
}

void inEax()
{
	int a = 3;
	asm("movl %%eax, %0;" : "=r" (a) :: "%eax");
	printf("%d\n", a);

}

void setFlag()
{
	asm("movl %0, %%eax;"
		:: "r" (getFlag())
		: "%eax");

	int a;
	asm("movl %%eax, %0;" : "=r" (a) :: "%eax");
	printf("%d\n", a);
}

void compare()
{
	asm("cmpl %0, %%eax;"
		"je eq;"
		"jg lrg;"

		"call smaller;"
		"jmp out;"

		"lrg:"
		"call larger;"
		"jmp out;"

		"eq:"
		"call equal;"

		"out:"
		:: "r" (getCurr()));	
}

void smaller() { printf("smaller\n"); }

void larger() { printf("larger\n"); }

void equal() { printf("equal\n"); }

int getFlag() { return 9; }

int getCurr() { return 3; }

Was This Post Helpful? 0
  • +
  • -

#5 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,113
  • Joined: 03-April 12

Re: Help with comparing 2 numbers

Posted 24 October 2015 - 12:23 AM

Try printing them all in compare().
I guess the compiler is free to use the registers as it likes between the asm-blocks. The compiler can even optmize the asm-blocks away. Thats why
'asm volatile' is often used.

First thing could be checking the generated assembly.
Try 'objdump' or listing-flags.

This post has been edited by turboscrew: 24 October 2015 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1