Murus: Cardinals
This Module is about some basic functions of the Cardinal type: multiplication and division for 32 bit numbers, written in assembly language. The DEF unit is small and so is the assembler unit.
Cardinals.def
This is a foreign module, so the actual module doing the work is written in something else than Modula-2. In this case, assembly language is used.
FOREIGN MODULE Cardinals; (* (c) Christian Maurer v. 5. April 2005 *) (* Edited by Jan Verhoeven, Aug 2008 *) PROCEDURE Multiply (VAR a, b : CARDINAL); (* a' := (a * b) MOD 2^32 and b' := (a * b) DIV 2^32. *) PROCEDURE Divide (VAR a, b : CARDINAL; c : CARDINAL); (* a' := (a + 2^32 * b) DIV c and b' := (a + 2^32 * b) MOD c. *) END Cardinals.
Cardinals.s
This is the assembler listing. In the top section, the labels to be exported are defined with the 'globl' keyword. Then the two routines doing the integer math are setup.
# Cardinalkern.s
#
# (c) Christian Maurer v. 5. April 2005
# Nutzungsbedingungen siehe Murus.mod
# Edited by Jan Verhoeven, Aug 2008
.text
.globl Multiply
.globl Divide
.align 4
Multiply:
pushl %ebx
movl 8(%esp), %eax # eax := ADR (a)
movl (%eax), %eax # eax := a
movl 8(%esp), %ecx # ecx := ADR (a)
movl 12(%esp), %ebx # ebx := ADR (b)
mull (%ebx) # eax := (a * b) MOD 2^32i
# edx := (a * b) DIV 2^32
movl %eax, (%ecx) # a := (a * b) MOD 2^32
movl %edx, (%ebx) # b := (a * b) DIV 2^32
popl %ebx
ret
Divide:
pushl %ebx
movl 8(%esp), %eax # eax:= ADR (a)
pushl %eax # ADR (a) auf den Stack
movl (%eax), %eax # eax:= a
movl 16(%esp), %ebx # ebx:= ADR (b)
movl (%ebx), %edx # edx:= b
movl 20(%esp), %ecx # ecx:= c
divl %ecx # eax:= (a + 2^32 * b) DIV c
# edx:= (a + 2^32 * b) MOD c
popl %ecx # ecx:= ADR (a) vom Stack
movl %eax, (%ecx) # a:= (a + 2^32 * b) DIV c
movl %edx, (%ebx) # b:= (a + 2^32 * b) MOD c
popl %ebx
ret
Page created 28 August 2008,
Page equipped with FroogleBuster technology