This is my assignment:
For each of these questions, you must write a C (not C++) program that contains three parts:
A. Some C code to read inputs (using scanf).
B. A segment of inline-assembler to do the computation.
C. Some C code to write the output (using printf).
- sign.c : reads a single integer, uses assembler to compute its sign (-1, 0, or 1), and outputs the sign. You will need opcodes jge and je.
I was able to do the first question (adding 2 integers, using those same 3 guidelines), but now I am stumped on this one.
I am able to get the first part of this particular question working (comparing eax and 0, then jumping to DoIF), but when I uncomment the other parts, the computation is messed up.
Thank you in advance for any help. It is greatly appreciated.
#include "stdafx.h"
int v; //integer to be read
int sign; //sign
int main()
{
printf("Enter an integer to compute its sign.\n");
scanf_s("%d", &v); // this is how you read an int from the user
//sign = -(v < 0); //C version
__asm // tell MS Visual studio to start a block of assembler
{
mov eax, v // load x into A
cmp eax, 0 // compare A with value 0
je DoIF // if comparison was = 0, jmp to DoIF
mov eax, 100
mov sign, eax
jmp done
//mov eax, v; // load x into A
//cmp eax, 1; // compare A with value 1
//jge OtherIF; // if comparison was = 0, jmp to DoIF2
//mov eax, 2;
//mov sign, eax;
//jmp done;
//mov eax, v; // load x into A
//cmp eax, -1; // compare A with value 1
//jle NextIF; // if comparison was <= 0, jmp to DoIF3
//mov eax, 100;
//mov sign, eax;
//jmp done;
DoIF:
mov eax, 0; // do y = 1
mov sign, eax;
/*OtherIF:
mov eax, 1;
mov sign, eax;*/
/*NextIF:
mov eax, -1;
mov sign, eax;*/
done:
}