I'm trying to write some simple asm code for practice but I get this error: error C2443: operand size conflict
I only want to add 2 numbers and then move the result to another variable.
#include<stdio.h>
int main() {
int a = 3, b = 3, c;
__asm {
mov ax, a;
mov bx, b;
add ax, bx;
mov c, ax;
}
printf("%d", c);
}
Related
I am a little confused as to what I need to fix to handle the error Exception thrown at 0x00AFF748 in ProjectTwo.exe: 0xC0000005: Access violation executing location 0x00AFF748.
I am attempting to write an external assembly language program that is then called by a C++ program. I am unsure if I messed up the assembly language code and that is why I keep getting the error. The goal of my program is
int problem1_ ( )
{
int numberArray [4] = {1, -3, 12, 15};
int result = 0, index = 0;
int numElements = 4;
while (index < numElements)
{
if ( index >= 1 && numberArray[index] > 2 )
{
result = result * numberArray[index] ;
}
else
{
result = result - 5;
}
index++;
}
return result;
}
My C++ Code:
#include<iostream>
using namespace std;
extern "C" int problem1_();
int main()
{
//Declare the variable and set it to returned result
//From calling the assembly program
int x = problem1_();
//Print the results
cout << "The result is: "<< x << endl;
return 0;
}
My assembly Language code:
.model flat, c
.data
numberArray DWORD 1, -3, 12, 15
numElements DWORD 5
.code
problem1_ proc
mov eax, 0 ;value of eax is 0
lea ebx, numberArray ;Load the array's memory address into register ebx'
mov ecx, numElements ;ecx is count down counter
cmp ecx, 0 ;If ecx not greater than 0, jump to quit
jng FINISH
iterateThruArray:
cmp[ebx], eax ;Compare value at current index to eax
jl result1
jmp result2
result1:
mul numberArray
jmp FINISH
result2:
sub eax, 5
jmp FINISH
FINISH:
pop ebp ; pop ebp from the stack
ret ; return to calling program
problem1_ endp
end
I create this piece of code in VS (C++)
#include<iostream>
using namespace std;
static short arr[10];
void powers() {
_asm {
mov ecx, 0;
mov dx, 0;
for:
mov ax, cx;
inc ax;
mul ax;
mov[arr + 2 * ecx], ax;
inc ecx;
cmp ecx, 10;
jl for;
}
}
Now I want to create another function which prints "Pow (x) = x^2", I stuck here:
void print_power(unsigned short x) {
const char* f = "Pow(%d) = %d \n";
_asm {
call powers
push f
add esp, 4
}
}
When I call my "powers" function from "print_power" -> my arr[10] gets filled with the ^2 of 1 to 10
(arr[0] = 1, arr[1] = 4, arr[2] = 9, arr[3] = 16, arr[4] = 25 .....) (I think)
I want when I call my print_power(x) function from main(), for example print_power(8) -> to print the 8th element from my arr like this: "Pow (8) = 81".
Thank you in advance, also I want to apologize if I have some mistakes.
If I mov, eax 12345 and later mov var, eax (assuming var is a 32bit int etc..etc..) and output var later it'll output correctly.
Same with ax. mov ax, 65535 -> mov var16, ax will output correctly.
But if I mov into al or even ah it won't output anything.
Perhaps moving into ah not working makes sense. But why is that so for both?
typedef int int32;
typedef unsigned char int8;
typedef short int16;
int _tmain(int argc, _TCHAR* argv[])
{
int8 c1 = 0;
int8 c2 = 0;
__asm
{
mov al, 255
mov c1, al
}
cout << c1 << endl; //outputs nothing, same if I used ah
//whereas
int32 n = 0;
__asm
{
mov eax, 10000
mov n, eax
}
cout << n << endl; // works fine, well, you get the picture
return 0;
}
The problem is not (only) with your assembly code, it's the way you print your value:
cout << c1 << endl;
Even though c1 is unsigned char (aka uint8), C++ iostreams treat it the same way they treat an ordinary char. In other words, that code is roughly equivalent to:
printf("%c\n", c1);
Just cast it to unsigned int and it should work fine.
BTW, on some platforms (e.g. Linux PowerPC) the ordinary char is actually unsigned.
256 equals to 2^8
when you use int8 , it uses twos complement approach , so the number is between [-128 , 127]
I think if you change its type to unsigned integer , it must work
Im trying to check the even numbers from an array inside a struct, but i dont think i wrote something right. When debugging, (i.e. count = 3 v[] = {1,2,4}), after it reaches " cmp eax,[ebp+12] and je outt; " , it goes to outt: and thats it.
s is supposed to keep the sum of all even numbers, eax inside int suma(test *) is index for array, and edx keeps the sum before moving it in s
what am i doing wrong?
#include "stdafx.h"
#include <iostream>
using namespace std;
struct test {
int v[10];
short count;
};
test a;
int s = 6;
int suma(test *)
{
_asm {
mov eax, 0; // i for counting inside array
mov edx, 0; // sum of even elements
mov ebx, [ebp + 8]; // array v adress
loop:
cmp eax, [ebp + 12];
je outt;
mov ecx, [ebx + 4 * eax];
inc eax;
mov edi, ecx
and ecx, 1;
cmp ecx, 1;
je loop;
add edx, edi;
jmp loop;
outt:
mov eax, edx;
}
return s;
}
int main()
{
cin >> a.count;
for (int i = 0; i < a.count; i++)
cin >> a.v[i];
_asm {
LEA eax, a
push eax;
call suma;
add esp, 4;
mov s, eax;
}
cout << s;
return 0;
}
I get a different return value each time, so I'm doing something wrong. If I replace the add with a basic inc, it returns correctly.
Here is the code.
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <iostream>
using namespace std;
int Add ( int _Number1, int _Number2 );
int main ( int _ArgumentCount, char * _Arguments[] )
{
int nInput, nOutput;
nOutput = Add ( 1, 1 );
cout << "1 + 1 = " << nOutput << endl;
cin >> nInput;
return 0;
}
__declspec ( naked ) int Add ( int _Number1, int _Number2 )
{
__asm xor eax, eax
__asm mov eax, _Number1
__asm add eax, _Number2
__asm ret
}
Here is the new, working code:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <iostream>
using namespace std;
int Add ( int Number1, int Number2 );
int main ( int ArgumentCount, char * Arguments[] )
{
int nInput, nOutput;
nOutput = Add ( 1, 1 );
cout << "1 + 1 = " << nOutput << endl;
cin >> nInput;
return 0;
}
int Add ( int Number1, int Number2 )
{
__asm mov eax, Number1
__asm add eax, Number2
}
__declspec (naked) means the function is created without any prolog or epilog code -- so if you want to access formal parameters, you need to write prolog code of your own to give you access to them. Your xor is also accomplish nothing, since you immediately overwrite eax with another value. Also note that any identifier starting with an underscore followed by a capital letter is reserved, so your code had undefined behavior. Try rewriting your function as:
int Add ( int number1, int number2 )
{
__asm mov eax, number1
__asm add eax, number2
}
or else write the code to access the parameters on the stack without depending on a prolog:
__declspec (naked) int Add2(int number1, int number2) {
_asm mov eax, [esp+4]
_asm add eax, [esp+8]
_asm ret
}
I didn't check, but I'd guess your original code was trying to load the parameters from [ebp+8] and [ebp+12]. This depends on the normal prolog code:
push ebp
mov ebp, esp
...which your __declspec (naked) specifically told the compiler not to generate.