Jump to content
IGNORED

Division with quotient and remainder in ASM


artrag

Recommended Posts

I was trying to implement in ASM an integer division with quotient and remainder.

I came with this loop, but again something has to be wrong...

Any suggestion on what could be wrong ?

	MVI var_INP,R0
	MVII #-1,R4				
Loop:
	INCR R4
	SUBI #20,R0
	BC Loop
	ADDI #20,R0
	MVO R0,var_N		; N = INP%20
	MOVR R4,R0
	MVO R0,var_M		; M = INP/20

 

Link to comment
Share on other sites

9 hours ago, artrag said:

Once again this code is just fine.

The bug was in the passed data.

 

Hi, @artrag, I'm sorry I missed your post initially.  I'm glad you sorted it out.

 

If you are using IntyBASIC, you may want to leverage the functions it provides already.  The division function it has discards the remainder, but it is available in a register.

 

Also, you don't have to copy to RO in order to write to memory.  You can just move R4 directly.  That is, unless there is a specific reason you want to return the quotient in R0 at the end.

 

ADDI #20,R0
	MVO R0,var_N		; N = INP%20
	MVO R4,var_M		; M = INP/20

    -dZ.

Link to comment
Share on other sites

4 hours ago, artrag said:

Thanks, actually in Intybasic integer division does not compute the reminder, so you cannot take it from a register.

I've done a ASM procedure to return the result in RO, so no more saving in M

 

Have you tried this in Intybasic? 

 

A=A%B             Simple unsigned remainder

  • Like 1
Link to comment
Share on other sites

Yes, but as I need both the reminder and the quotient, if you do A%20  and A/20 intybasic will do twice the work (two loops with successive subtractions).

 

If you add this code to your program an call m = usr DIVREM20(addr) you will get in N the reminder and in M the quotient

ASM	DIVREM20:	PROC
ASM			MVII #-1,R4				
ASM	DivLoop20:
ASM			INCR R4
ASM			SUBI #20,R0
ASM			BC DivLoop20
ASM			ADDI #20,R0
ASM			MVO R0,var_N		; N  = INP%20
ASM			MOVR R4,R0          ; R0 = INP/20
ASM			JR R5
ASM			ENDP

 

Link to comment
Share on other sites

34 minutes ago, artrag said:

Yes, but as I need both the reminder and the quotient, if you do A%20  and A/20 intybasic will do twice the work (two loops with successive subtractions).

 

If you add this code to your program an call m = usr DIVREM20(addr) you will get in N the reminder and in M the quotient


ASM	DIVREM20:	PROC
ASM			MVII #-1,R4				
ASM	DivLoop20:
ASM			INCR R4
ASM			SUBI #20,R0
ASM			BC DivLoop20
ASM			ADDI #20,R0
ASM			MVO R0,var_N		; N  = INP%20
ASM			MOVR R4,R0          ; R0 = INP/20
ASM			JR R5
ASM			ENDP

 

I’ll have to look at the code, but I believe that the Modulo operation discards the quotient.  It just doesn’t give it to you in IntyBASIC, but you should be able to call it  and switch to assembly right after.

 

    dZ.

Link to comment
Share on other sites

5 minutes ago, artrag said:

My code is enough. Intybasic isn't optimal for this task

I understand.

 

By the way, since you are digging into Assembly, I will recommend to avoid reading and writing to memory constantly like IntyBASIC does.  It is more expensive than just accepting and returning values in registers.

 

And if you need to access memory, you should optimize for indirect access (MVO@, MVI@, ADD@, etc.) especially for repeated accesses, because direct memory access is a bit more expensive.
 

On subroutines, because they are made to be called repeatedly, these can add up.

 

    dZ.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...