visual-c++,assembly,macros , Macro to push arguments onto stack

Macro to push arguments onto stack


Tag: visual-c++,assembly,macros

I've been working on a fun little project to mess around with how functions are called and I need a macro to push arguments as it'll be quite time consuming to push the arguments manually for every instance of this obfuscated call.

This is my code so far:

#define pushargs(...)                      \
    // idk what to do here...

#define ObfuscatedPrefix(FunctionPtr)      \
    __asm cmp ebx, FunctionPtr             \
    __asm _emit 0x74 __asm _emit 0x02      \
    __asm _emit 0xEB __asm _emit 0x03      \
    __asm mov [ebp + 4], eax

#define DecryptOffset(FunctionPtr, V)      \
    ((FunctionPtr) - (V))

#define ObfuscatedCall(FunctionPtr, V, ...)\
    __asm push ebx                         \
    __asm push eax                         \
    __asm mov ebx, FunctionPtr             \
    __asm mov eax, V                       \
    __asm sub ebx, eax                     \
    pushargs(__VA_ARGS__)                  \
    __asm _emit 0xEB __asm _emit 0x02      \
    __asm _emit 0xEB __asm _emit 0x05      \
    __asm _emit 0xE8 __asm _emit 0xF9      \
    __asm _emit 0xFF __asm _emit 0xFF      \
    __asm _emit 0xFF __asm pop eax         \
    __asm add eax, 0x07                    \
    __asm jmp ebx                          \
    __asm pop ebx

I feel like this isn't possible, please correct me if I'm wrong...


If you are still interested in tricking the Visual C++ compiler you can try this code

#define pushargs(...) for (unsigned int _esp; ;) _esp = varcount(), _esp =(_esp-varcount(__VA_ARGS__))>>2, pushargs_c(_esp, __VA_ARGS__ ); 

unsigned int __declspec(naked) __forceinline varcount(...)
    __asm mov eax, esp;
    __asm ret;

unsigned int __declspec(naked) __forceinline pushargs_c(unsigned int count, ...)
    __asm mov eax, DWORD PTR [esp+04h]
    __asm dec eax
    __asm shl eax, 2
    __asm sub esp, eax
    __asm push DWORD PTR [esp+eax]
    __asm ret;

My version of Visual Studio (2013 express) doesn't inline the calls even with __forceinline.
This code is not portable and is implementation specific, I found it working with my VC++ compiler, even in Release mode but may breaks under heavy optimizations or in new Visual Studio versions!

Give it a try, you can use the pushargs macro as you expect. pushargs_c is more reliable but require the first argument to be the number of the remaining arguments (i.e. pushargs_c(5, 1,2,3,4,5)).


NASM: copying a pointer from a register to a buffer in .data

I am new to asm. I am trying to copy a pointer from a register to a .data variable using NASM, on linux 64-bit. Concider this program: section .data ptr: dq 0 section .text global _start _start: mov [ptr], rsp mov rax, 60 mov rdi, 0 syscall Here I try...

MinGW's ld cannot perform PE operations on non PE output file

I know there are some other similar questions about this out there, be it StackOverflow or not. I've researched a lot for this, and still didn't find a single solution. I'm doing an operative system as a side project. I've been doing all in Assembly, but now I wanna join...

How to use namespace System; in MFC application with VC++

help me,am new, I want to use some features(i don't know the specific word to use) present in System namespace, but when i try to add it to my project by "using namespace System",i want this to use SQLCLIENT ,i saw below link giving answer but dont have a clue...

Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer?

I am new to assembly language. I make this code just now and run it with no errors so far, except It will only give a result from 1 - 100, here's my code. This is a simple math operation which is addition and subtraction. I tried analyzing the code...

link.exe returns error LNK1181: cannot open input file 'C:\Program.obj'

I'm compiling with a Batch file containing @echo off set link=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\link "%link%" external_input.obj periodic_dinger.obj flagger.obj monitor.obj main.obj libGui.lib libCore.lib libCint.lib libRIO.lib libNet.lib libHist.lib libGraf.lib libGraf3D.lib libGpad.lib libTree.lib libRint.lib libPostscript.lib libMatrix.lib libPhysics.lib libMathCore.lib libThread.lib All the objects and libraries are in the same directory, which is...

What does DX + 2 mean in mov ah,9 int 21h?

mov dx, offset buffer mov ah, 0ah int 21h jmp print buffer db 10,?, 10 dup(' ') print: xor bx, bx mov bl, buffer[1] mov buffer[bx+2], '$' mov dx, offset buffer + 2 mov ah, 9 int 21h I know buffer[bx+2] stands for '$', but offset buffer +2 in mov...

How to jump to an address saved in a register in intel assembly? [duplicate]

This question already has an answer here: conditional jump to register 1 answer say I calculated an address of a label and stored it in eax register, how can I JMP(specifically JE) to eax? jmp eax compiles, but I didn't check if it works. je eax doesn't compile(invalid combination...

How do I check assembly output of Java code?

I found this question that answered it for C++: How do you get assembler output from C/C++ source in gcc?

Wrong answer from DIV assembly

I have a part of my code mov di,3 mov cx,16 looop: xor dx,dx shl bx,1 adc dx,dx cmp cx,16 je cx16 (it's will dec cx and jump loop) push dx dec cx cmp cx,0 je cx0 mov ax,cx div di cmp dx,0 jne looop when cx = 3 i...

Why a breakpoint jumps in my C++ code?

MS Visual Studio 2013. I see my breakpoint is moving in runtime (C++ code). I had never such behaviour in my C# code, why I get this in C++? How can I solve it? #include <Windows.h> int main(){ HANDLE hFile = ::CreateFile(TEXT(""), 0, 0, NULL, OPEN_EXISTING, 0, NULL); if (INVALID_HANDLE_VALUE...

Linker error compiling DX10 program in Visual Studio 2015

I am trying to compile old program which works with DirectX 10. Full list of libraries used: #pragma comment(lib, "d3d10.lib") #pragma comment(lib, "d3dx10.lib") #pragma comment(lib, "dxgi.lib") #pragma comment(lib, "dxerr.lib") #pragma comment( lib, "dxguid.lib" ) Linker error: dxerr.lib(dxerrw.obj) : error LNK2019: unresolved external symbol _vsnwprintf referenced in function "long __cdecl StringVPrintfWorkerW(unsigned...

How is a file loaded in the FAT file system?

I am developing a bootloader in x86 Assembly and I have a question about the FAT file system. Which steps are necessary to find and load the file?...

Using C callback in C++/CLI

What I have I'm using Vimba C API, in my C++/CLI application. It's known to work since my mate has it done and running without errors. I've all the imports/include done. I'm using the API without errors, except one. The Manual Says In the manual of the API, there's some...

Linq in C++ CLI

I need to get details from a xml file. I wrote code in C# but not able to rewrite in C++/CLI. Code in C# class cROI { public Int16 iX { get; set; } public Int16 iY { get; set; } public Int16 iWidth { get; set; } public Int16...

GCC emits vastly different code using “-march=native” on similar architectures

I'm working on writing an OpenCL benchmark in C. Currently, it measures the fused multiply-accumulate performance of both a CL device, and the system's processor using C code. The results are then cross checked for accuracy. I wrote the native code to take advantage of GCC's auto vectorizer, and it...

Which is better for MFC application hot key or Accelerator?

We have MFC application which has around 10 image buttons to which we want to provide shortcut keys. Shortcuts will be customizable. I have implemented shortcuts (with no customization right now) with hotkeys using ON_WM_HOTKEY() message. After searching through Goolge I am little confuse. For example, this question is suggesting...

Understanding NASM Macro

I've come around this macro in a assembly source file and I just can't figure out how it's working. So first I come around this function (hevc_deblock.h): cglobal hevc_v_loop_filter_chroma_8, 3, 5, 7, pix, stride, tc, pix0, r3stride sub pixq, 2 lea r3strideq, [3*strideq] mov pix0q, pixq add pixq, r3strideq TRANSPOSE4x8B_LOAD...

Visual C++ accepts One point decimal

I have a calculator in visual c++, and I want to press a button of point, example 1.2 point , but if the number already has a point don't accept more String^ punto; String^ dato = lblDatos->Text; bool point = false; if (lblDatos->TextLength > 0) { punto = dato +...

How to implement an interface defined in in VC++/CLI?

I have a based interface like this: Namespace Foo Public Interface Bar ReadOnly Property Quuxes as Quux() End Interface End Namespace I now want to implement this in VC++/CLI (because I need to interface functions from an unmanaged third-party DLL), however I cannot figure out the correct syntax how...

Declaring Variables in the .data Versus on the Stack - ASM

I'm trying to understand in which cases you would want to declare variables in the .data section of my assembly program and using it for the procedures needed, over instantiating local variables on the stack and vice versa. Is it just that declaring variables in the .data section is only...

Range of immediate values in ARMv8 A64 assembly

My understanding is that immediate parameters in ARMv8 A64 assembly can be 12 bits long. If that is the case, why does this line of assembly code: AND X12, X10, 0xFEF Produce this error (when compiled with gcc) Error: immediate out of range at operand 3 -- `AND X12, X10,...

Print string using INT 0x10 in bootsector

I want to create printl function that allow me to print string in the ax register. I am in 16-bit real mode and I can not find any way to print a message. I using int 0x10 to print a single letter. I try pass argument (string to print) in...

Why does C++ allow a semicolon at the start of a line? [duplicate]

This question already has an answer here: Why no semicolon gives errors but too many of them don't? 4 answers #include <iostream> using namespace std; int main() { ;;;;int i = 90;;;;; ; ;;;;cout<<"Hello"; return 0; } The above program compiled and ran successfully. From my understanding, the ;...

Is this an overflow, or maybe more keyboard data?

I am writing a bootloader, and it's functionality is basically limited to printing a string, then copying keyboard characters to the screen as they are typed. While writing the routines that read and write the key, I noticed my print routine was not detecting the null terminator in offset (plus)...

Counter not working after jumps - assembly language

For some reason, when i switch to mouse input switch back to keyboard input for my program, increasing and decreasing the counter has no effect. It works perfectly in the first loop where we input characters. Here is the program guys, any advice? look at whatspeed jump for reference after...

Asm x86 segmentation fault in reading from file

I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

Range of Addresses for a Conditional Branch Instruction in MIPS

What is the range of addresses for a conditional branch instruction in MIPS with respect to X, where X contains the address of the branch instruction? Assume the current PC value is 0x10000000. What is the range of addresses for jump (j) and the jump and link (jal) in- structions...

How is shellcode generated from C? - With code example

I started taking an online course regarding software security. In one of the sections, I was instructed to execute a hidden C function using a buffer overflow. I got to thinking: what would happen if I could pass machine instruction directly to a stack unsafe executable? What I have been...

Is it possible to override IEnumerable in VC++/CLI?

I have an interface which returns an IEnumerable, and I want to implement this in VC++/CLI because the data comes from a third-party unmanaged DLL. So far I have: public ref class MyEnumerable : IEnumerable<SomeType^> { public: virtual IEnumerator<SomeType^>^ GetEnumerator(); } But the compiler complains with C2393: "Covariant returns types...

storing array from user and accessing it

I have to create an int array based on user input, sum the integers, and output the sum and the array itself. My code calculates the sum correctly, but I cannot output the list. It only prints out the last number and zeros and then gets stuck in an infinite...

How can I access the individual elements of an array in a loop?

I need to print the cells of an array, I have an array which contains the word "HELLO_WORLD", I manage to print an index by its own but I can't manage to print all the cells one by one, here is the code : loop: la $t0, hexdigits # address...

ARM assembly cannot use immediate values and ADDS/ADCS together

I am currently trying to speed up some of my C functions on a Cortex-M0 (Freescale KL25Z) using assembly. I get a problem with this minimal test program: @.syntax unified .cpu cortex-m0 .text .global test .code 16 test: mov r0, #0 adds r0, r0, #1 bx lr When I try...

subl causing Floating point exception

I am creating a compiler which should compile a fictional language into asm x86 code. When compiling this piece of code (fictional code): int x; int f(int n) { write n; } int main() { x = 1; f(x); } write is equal to just print in console. The desired...

Visual Studios building and debugging .cpp file without main()

Professor just gave us a block of c++ code in order to learn about the debug windows in Visual Studio, however after creating a blank win32 console project and then dragging in the .cpp file I get the missing .exe error message when attempting to debug. I've looked everywhere and...

assembly function with C segfault

I am trying to make assembly function that uses SSE and FPU for parallel calculations. Unfortunately I am receiving segmentation fault(core dumped) error(while debugging it doesn't show in assembly function). I also cannot step out from assembly function. Gdb shows: Warning: Cannot insert breakpoint 0. Cannot access memory at address...

Error: Junk at EOL, first unrecognised character is '('

I am trying to compile Quake 1 (QW) from source code and have run into the following problem when compiling client/d_copy.s : Error: Junk at EOL, first unrecognised character is '('. The file in question is here (please excuse the syntax highlighting, pastebin only supports NASM). I am attempting to...

Reserve bytes in stack: x86 Assembly (64 bit)

pushq %rbp movq %rsp, %rbp subq $32, %rsp I have big question regarding explanation of "$32" in third instruction. The information from search and blogs specifies that in above third instruction we are reserving certain "bytes" of space for stack. From doc, %rsp is 64 bit register and %esp is...

C++ Why does this work

I am using Microsoft Visual studio 2013 I currently have this switch statement: switch (option) { case 1: getline(cin, newname); cout << "What would you like your new username to be?\nName: "; getline(cin, newname); name = newname; cout << "\nYour username is now '" << name << "' with your...

Add more features to stack container

I am using default features(push, pop, top, empty, size) of stack container of STL. If I want to add more features like access an element from middle of stack. How could I do this? Thanks...

Arithmentic operator(+) operator will check both return type and passing arguments or not?

#include<iostream> using namespace std; class B; class A { private: int a,b; public: A() { a=b=10; } void show() { cout<<"Hello of A: \n a:"<<a<<" b:"<<b; } friend A operator+( A AA ,B BB) ; friend B operator+ ( B BB,A AA); friend B operator+( A AAA ,B BB) ;...

NASM assembled bootloader memory issue

I am writing a bootloader with nasm. At the moment it is designed to output a welcome string, then record keystrokes while displaying them, printing the stored keystrokes upon finding the enter key, and finally halting. bits 16 org 0x7C00 start: jmp main bgetkey: pusha mov ax, 0 mov ah,...

C++ DLL does not run on different machine

I am developing a simple c++ application to generate a dll which later I am using in JNI to run native api calls, but the thing is the dll works well on the machine which I used to develop the dll but does not on the server. Both machines run...

LC3 assembly-unable to print the right character

I have been trying to make a program that decrypts a text that is being read from a file under the following condiditons: The adress of the first element of the file is 5001.Each letter is encrypted twice:The first time with Ceasar encryption and the second time with XOR encryption.The...

Google Chrome - How to compile Google Chrome in Windows?

Documentation mentioned to compile i have to follow this instruction for Windows: So i did all the steps, but the following is failing how can i resolve it? C:\tpt\depot_tools>mkdir out\Debug C:\tpt\depot_tools>ninja -C out\Debug chrome ninja: error: loading '': Het systeem kan het opgegeven bestand niet vinden. ninja: Entering directory...

Macro to push arguments onto stack

I've been working on a fun little project to mess around with how functions are called and I need a macro to push arguments as it'll be quite time consuming to push the arguments manually for every instance of this obfuscated call. This is my code so far: #define pushargs(...)...

Why is a “user breakpoint” called when I run my project with imported .lib, not when code is inline?

The Situation I am writing a wrapper library for GPIB communications for setting up specific instruments according to a clients specifications to automate some of their processes. I have to use C++ and the old '98 compiler in VC++ 6.0 running on a Windows NT machine to maintain compatibility with...

Visual Studio 2013 LINK : fatal error LNK1181: cannot open input file

I am using Visual Studio 2013. I'm trying to build some code given to me from my professor and I keep getting this error: LINK : fatal error LNK1181: cannot open input file 'C:\Users\manduchi\Documents\eyegaze\EyeGazeDemo..\Libraries\OpenCV\lib\opencv_core249.lib' However, on my computer opencv_core249.lib is located somewhere else. I've tried updating the linker directories to...

How does this instruction look in memory?

I am having trouble figuring out how this instruction looks in memory for a x86 processor. mov $0x3c,%eax Can someone help me figure it out? For example an easy one is: xor %edi,%edi ---> 0x31 0xFF...

Make the input wait for mouse or keyboard - Assembly Language

I fixed my program, but now the loop seems to be stuck. Whenever I press u or d, it is stuck, but the mouse part works :) ; You may customize this and other start-up templates; ; The location of this template is c:\emu8086\inc\0_com_template.txt org 100h CLEAR MACRO MOV AX,0600H...