So I want to convert this C language program to Assembly:

  void main
  {
    int year;
    printf("Enter the year: ");
    scanf("%d",&year);

     if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
    {
        printf("Year %d is a leap year",year);
    }
    else
    {
        printf("Year %d is not a leap year",year);
    }
  }

Can you help me figure out how it is mapped in Asm? I tried to convert it using this link: but I'm having an error: Error: Command failed: /tmp/test683852013.c:2:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘{’ token

I would gladly appreciate your help. Thanks.

Dani AI

Generated

Quick diagnosis: the conversion tool failed because the C source was syntactically invalid — the function header is missing parentheses, so the C parser dies before any assembly can be emitted. already spotted the immediate fix, and is correct that letting the compiler produce assembly is the most practical way to see how the C maps to ASM.

A minimal, portable entry point is:

int main(void)
{
    /* include <stdio.h> and keep year as an int */
}

Using a standard signature avoids compiler complaints and undefined behavior that some toolchains flag for void main.

Recommended workflow (produce readable ASM, then hand-edit if desired):

# produce assembly (Intel syntax), no optimizations so the mapping is obvious
gcc -Wall -Wextra -O0 -S -masm=intel -o leap.s leap.c

# if you specifically want 32-bit output on a 64-bit host
gcc -m32 -Wall -O0 -S -o leap_i386.s leap.c

What to expect and a few cautions: with -O0 the compiler emits a clear prologue/epilogue and explicit calls to scanf/printf; integer % operations show up as idiv (remainder ends up in edx/rdx). With optimizations the compiler often replaces year % 4 by a bitwise and when safe, so the optimized ASM can look quite different. On x86-64 remember calling conventions (first args in registers) and the requirement that the stack be 16-byte aligned before calling variadic functions like printf or scanf. The fastest, most reliable path is: fix the C syntax, generate .s with the compiler for your target (32 vs 64-bit), study that output, then adapt or hand-write assembly preserving the platform calling convention and alignment.

Recommended Answers

All 2 Replies

some compilers have an option to produce an assembly code. What compiler do you use?

just insert () after void main

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.