Search code examples
assemblyx86masminline-assembly

OFFSET keyword for printing numbers in masm


I wrote code with MSVC inline assembly where I print char arrays by printf using offset. It works fine.

#include <iostream>
using namespace std;
char FORMAT[] = "%s %s %s %s, %s\n";
char SURNAME[] = "Ponomarenko";
char NAME[] = "Maria";
char DESIGN[] = "Design";
char BY[] = "by";
char YEAR[] = "2020";
int YEAR1 = 2020;

void main() {
__asm { 
    mov eax, offset YEAR
    push eax
    mov eax, offset SURNAME
    push eax
    mov eax, offset NAME
    push eax
    mov eax, offset BY
    push eax
    mov eax, offset DESIGN
    push eax
    mov eax, offset FORMAT
    push eax
    mov edi, printf
    call edi

    pop ebx
    pop ebx
    pop ebx
    pop ebx
    pop ebx
} 
system("pause");
} 

Than I tried to print number YEAR1, I tried this

mov eax, offset YEAR1
    push eax

and the result was weird, and then I wrote without offset and it worked! (of course in both cases I changed my FORMAT array)

mov eax, YEAR1
    push eax

Can you explain why offset affects printing numbers this way?


Solution

  • in visual studio, microsoft assembler (MASM) is used where offset loads the address of the variable, not the value itself, you can use offset to invoke a function but not load a value.

    please use this link as a reference: http://www.asmcommunity.net/forums/topic/?id=15124