artrag Posted April 24, 2022 Share Posted April 24, 2022 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 Quote Link to comment Share on other sites More sharing options...
artrag Posted April 24, 2022 Author Share Posted April 24, 2022 Once again this code is just fine. The bug was in the passed data. 1 Quote Link to comment Share on other sites More sharing options...
+DZ-Jay Posted April 25, 2022 Share Posted April 25, 2022 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. Quote Link to comment Share on other sites More sharing options...
artrag Posted April 25, 2022 Author Share Posted April 25, 2022 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 Quote Link to comment Share on other sites More sharing options...
+cmadruga Posted April 25, 2022 Share Posted April 25, 2022 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 1 Quote Link to comment Share on other sites More sharing options...
artrag Posted April 25, 2022 Author Share Posted April 25, 2022 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 Quote Link to comment Share on other sites More sharing options...
+DZ-Jay Posted April 25, 2022 Share Posted April 25, 2022 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. Quote Link to comment Share on other sites More sharing options...
artrag Posted April 25, 2022 Author Share Posted April 25, 2022 My code is enough. Intybasic isn't optimal for this task Quote Link to comment Share on other sites More sharing options...
+DZ-Jay Posted April 25, 2022 Share Posted April 25, 2022 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.