How Do Divisions and Modulus Work in Assembly for Calculating Days?

  • Thread starter Thread starter dohsan
  • Start date Start date
  • Tags Tags
    Assembly
Click For Summary
SUMMARY

This discussion focuses on the implementation of division and modulus operations in x86 assembly language for calculating the day number of a given date. The formulas provided include adjustments for the month and year, specifically using the expressions m = (m + 9) % 12 and y = y - m/10. The assembly code demonstrates how to perform these calculations using the DIV instruction, which is essential for obtaining the quotient and remainder in the absence of a direct modulus operator. The example given calculates the day number for April 1, 2011, confirming the accuracy of the formulas through manual verification.

PREREQUISITES
  • Understanding of x86 assembly language syntax and instructions
  • Familiarity with the DIV instruction for division operations
  • Knowledge of basic date calculations and leap year rules
  • Experience with MASM or similar assemblers for macro commands
NEXT STEPS
  • Research the use of macros in MASM for implementing modulus operations
  • Study the implementation of date calculations in assembly language
  • Learn about handling leap years in date algorithms
  • Explore optimization techniques for assembly code performance
USEFUL FOR

This discussion is beneficial for students learning assembly language, software developers working on low-level date calculations, and anyone interested in optimizing mathematical operations in x86 assembly.

dohsan
Messages
7
Reaction score
0

Homework Statement


I'm writing a program to calculate the number of days. I'm really confused on how divisions or modulus works in assembly... but I tried =/

Given that the day, month, and year value of a particular date are d, m, y. The day number of that date is calculated:

m = (m + 9) % 12
y = y - m/10
day_num = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )

Homework Equations


The Attempt at a Solution


;month 1
mov eax, m1
add eax, 9
mov edx,0
mov ecx,12
div ecx
mov eax, 0
mov m1,edx

;month 2
mov eax, m2
add eax, 9
mov edx,0
mov ecx,12
div ecx
mov eax, 0
mov m2,edx
 
Physics news on Phys.org
In x86 assembly, there isn't a modulus operator, but there might be a macro command for it in MASM. I did most of my x86 assembly some years back, using the Borland assembler, TASM.

Where did you get these formulas?
dohsan said:
m = (m + 9) % 12
y = y - m/10
day_num = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )

Let's consider a day this year, Apr 1, 2011.
There were 31 days in January, 28 days in Feb (not a leap year), and 31 days in Mar, so the day number of Apr 1 should be 31 + 28 + 31 + 1 = 91.

From your formulas, m = (4 + 9) % 12 = 13 % 12 = 1 ?
y = 2011 - 1/10 = 2011
I'm not even going to attempt to calculate the day from your formula.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K