Question

int getFirstDayOfMonth(int mon, int year) {    int adjusted_month = (mon + 9) % 12 +...

int getFirstDayOfMonth(int mon, int year)
{

   int adjusted_month = (mon + 9) % 12 + 1;
  
   int adjusted_year = year - 2000;
   if(adjusted_month > 10)
       adjusted_year--;

   // declare day, set to 1
   int day = 1;
   // declare century, set to 20
   int century = 20;
   // declare output, set to
  
   // (13 * mon - 1) / 5 + year / 4 + century / 4 + day + year - 2 * century;
   int output = (13 * adjusted_month - 1) / 5 + adjusted_year / 4 + century / 4 + day + adjusted_year - 2 * century;
  

   // mod output by 7
   output = output%7;
   // add 7 to output
   output = output + 7;
   // mod output by 7 again
   output = output%7;

   // return output

   return output;

}

Please convert above C program to x86. Please post the correct solution. TIA,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given, Asked to convert the given c code to x86 assembly code.

Explanation: I have used x86 GCC compiler to check the method result and it is working properly. Please verify the code and let me know if it works for you.

Given Code:

int getFirstDayOfMonth(int mon, int year)
{
int adjusted_month = (mon + 9) % 12 + 1;
int adjusted_year = year - 2000;
if(adjusted_month > 10)
adjusted_year--;
int day = 1;
int century = 20;
int output = (13 * adjusted_month - 1) / 5 + adjusted_year / 4 + century / 4 + day + adjusted_year - 2 * century;
output = output%7;
output = output + 7;
output = output%7;
return output;
}

x86 Code:

getFirstDayOfMonth:
pushl %ebp
movl %esp,%ebp
subl $28,%esp
pushl %ebx
pushl %esi
pushl %edi
movl 8(%ebp),%eax
addl $9,%eax
movl %eax,-24(%ebp)
movl $12,%ecx
cltd
idivl %ecx
movl %edx,-28(%ebp)
movl %eax,-24(%ebp)
incl %edx
movl %edx,-4(%ebp)
movl $1,-8(%ebp)
movl $20,-12(%ebp)
movl 12(%ebp),%eax
addl $-2000,%eax
movl %eax,-16(%ebp)
cmpl $10,-4(%ebp)
jle .L2
decl -16(%ebp)
.L2:
imull $13,-4(%ebp),%eax
movl %eax,-24(%ebp)
decl %eax
movl %eax,-24(%ebp)
movl $_divsi3,-28(%ebp)
pushl $5
pushl %eax
call _divsi3
leal 8(%esp),%esp
movl %eax,%esi
movl -16(%ebp),%edi
cmpl $0,%edi
jge .L3
leal 3(%edi),%edi
.L3:
sarl $2,%edi
leal (%edi,%esi),%esi
movl -12(%ebp),%edi
cmpl $0,%edi
jge .L4
leal 3(%edi),%edi
.L4:
sarl $2,%edi
leal (%edi,%esi),%ebx
addl -8(%ebp),%ebx
addl -16(%ebp),%ebx
movl -12(%ebp),%esi
sall $1,%esi
movl %ebx,%eax
subl %esi,%eax
movl %eax,-20(%ebp)
movl $7,%ecx
cltd
idivl %ecx
movl %edx,%esi
movl %eax,%ebx
movl %esi,-20(%ebp)
addl $7,-20(%ebp)
movl -20(%ebp),%eax
cltd
idivl %ecx
movl %edx,%esi
movl %eax,%ebx
movl %esi,-20(%ebp)
movl -20(%ebp),%eax
jmp .L1
.L1:
leal -40(%ebp),%esp
popl %edi
popl %esi
popl %ebx
leave
ret

Please let me know if you need more help in this.

Add a comment
Know the answer?
Add Answer to:
int getFirstDayOfMonth(int mon, int year) {    int adjusted_month = (mon + 9) % 12 +...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in C, need help implementing this function. This function gets the number of days in...

    Programming in C, need help implementing this function. This function gets the number of days in the given month int getFirstDayOfMonth(int mon, int year) { // Use Zeller's algorithm here. Pseudo code for Zeller’s algorithm: declare adjusted month, set to (mon + 9) % 12 + 1 declare adjusted year, set to year - 2000 if adjusted month is greater than 10: subtract one from adjusted year declare day, set to 1 declare century, set to 20 declare output, set...

  • please rewrite or convert this C program into Assembly language and please make a notice of...

    please rewrite or convert this C program into Assembly language and please make a notice of which function is which! #include <stdio.h> #include <stdlib.h> void printMonthYear(int mon,int year); void printDaysOfWeek(); void printDays(int mon,int year); int getFirstDayOfMonth(int mon, int year); int getNumOfDaysInMonth(int mon, int year); int main(void) {    int mon=-1; int year= -1; printf("Month : "); fflush(stdout); scanf("%d",&mon);    if(mon < 1|| mon > 12){ printf("Invalid month >.<!: %d ! Must be between 1 and 12\n",mon); return 1; } printf("Year...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • c++ please :) First, ask the user to enter a year (4-digit) and what week day...

    c++ please :) First, ask the user to enter a year (4-digit) and what week day does this year starts with (String) For example, the year 2018 starts with the week day "Friday". In this case, the calendar should start from January 1 which is Friday, 2018 Your program should produce a formatted calendar for the specified year and there should be a blank line after each month. Moreover, the month and the year should be centered over each month....

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Write a simple Python program to find the day for the given date. The procedure to...

    Write a simple Python program to find the day for the given date. The procedure to find the day of the week is as follows:    F = (K + (13 * m – 1) mod 5 + D + D mod 4 + C mod 4 – 2 * C ) mod 7 where, K : day of the month m : month number D : remainder when year divided by 100 C : quotient when year is divided...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!

    I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you! Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds to that date. Your program should take the input in numeric form, thus the input prompt should be as follows: Please enter a date below: Enter month (1-12): Enter day (1-31) Enter year...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT