Hi I am trying to compute the modulus of two numbers in assembly code. As I am more fluent in C++ it is hard for me to understand the concept of modulus as all I use is the symbol % to compute. The problem I am doing is computing the exponent of X to the Y power and then the modulus of this answer with Z. Where X, Y, and Z are one digit numbers input from the keyboard. This is what I have so far.

mov	ah,1
	int	21h          ;input X

	mov	bl,al	;move X to register bl

	mov	ah,1
	int	21h          ;input Y

	mov	cl,al	;move Y to register cl

	mov	ah,1
	int 	21h          ;input Z

	mov	dl,al	;move Z to register dl
	mov	al,bl	;move X back to al for multiplication

loop:			
	mul	al	;multiply X to itself 
	sub	cl,1	;decrement Y
	cmp	cl,1	;compare to see if Y is 1
	jne	loop	;if Y is greater than 1 jump to loop

	mod	al,dl	;???MODULUS??? I know this isn't       
                                                ;right, it is the part i need help with

	mov	dl,al	;move answer to dl

	mov	ah,2	
	int	21h	;ouput answer

Recommended Answers

All 3 Replies

If the operand is a byte value, DIV divides the contents of AX by the contents of the operand, then stores the result in AL and the remainder in AH. If the operand is a WORD value, DIV divides the contents of DX:AX by the contents of the operand, then stores the result in AX and the remainder in DX. This instruction treats numbers as unsigned binary values.

WORD EXAMPLE

mov ax, 5h ; number to divide goes in AX
mov bx, 2h ; dividing-by (operand)
div bx     ; perform division. <- WORD division
           ; Stores integer result in AL and remainder in DX
int 20h

;C:\science\asm>debug MODULUS.COM
;-r
;AX=0000  BX=0000  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0100   NV UP EI PL NZ NA PO NC
;14BD:0100 B80500        MOV     AX,0005
;-p
;
;AX=0005  BX=0000  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0103   NV UP EI PL NZ NA PO NC
;14BD:0103 BB0200        MOV     BX,0002
;-p
;
;AX=0005  BX=0002  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0106   NV UP EI PL NZ NA PO NC
;14BD:0106 F7F3          DIV     BX
;-p
;AX=0002  BX=0002  CX=000A  DX=0001  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0108   NV UP EI PL NZ NA PO NC
;14BD:0108 CD20          INT     20
;-p
;
;Program terminated normally
;-q

Byte Example

mov ax, 5h ; number to divide goes in AX
mov bx, 2h ; dividing-by (operand)
div bl     ; perform division. <- BYTE division
           ; Stores integer result in AL and remainder in DX
int 20h

; BYTE Example
;C:\science\asm>debug MODULUS.COM
;-r
;AX=0000  BX=0000  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0100   NV UP EI PL NZ NA PO NC
;14BD:0100 B80500        MOV     AX,0005
;-p
;
;AX=0005  BX=0000  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0103   NV UP EI PL NZ NA PO NC
;14BD:0103 BB0200        MOV     BX,0002
;-p
;
;AX=0005  BX=0002  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0106   NV UP EI PL NZ NA PO NC
;14BD:0106 F6F3          DIV     BL
;-p
;
;AX=0102  BX=0002  CX=000A  DX=0000  SP=FFFE  BP=0000  SI=0000  DI=0000
;DS=14BD  ES=14BD  SS=14BD  CS=14BD  IP=0108   NV UP EI PL NZ NA PO NC
;14BD:0108 CD20          INT     20
;-p
;
;Program terminated normally

...all of that means you get the modulus by calling DIV.
The modulus will be in AH (for BYTE division) or in DX (for WORD division).

Thank you that fixed my problem

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.