What is a reasonable amount of assembly code to implement gt, lt, eq?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

What is a reasonable amount of assembly code to implement gt, lt, eq?

tungsten
My implementation at the moment feels very clunky.

I am using 15 lines of assembly code, of which 7 are just to retrieve the two values to compare from the stack!

I suspect that it can be done in fewer lines...
Reply | Threaded
Open this post in threaded view
|

Re: What is a reasonable amount of assembly code to implement gt, lt, eq?

cadet1620
Administrator
Is 15 number of lines or number of instructions (labels don't count since they don't require ROM). In either case 15 isn't too bad.

The shortest I know of is 11 instructions.

(See this post for a way to cheat and do it in only 4 instructions. You are not expected to use this ASM subroutine technique.)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: What is a reasonable amount of assembly code to implement gt, lt, eq?

tungsten
Thank you! Will chew on the hint and see what I can do.

Without the labels it comes to 13 instructions. And with a minor tweak 12.
Reply | Threaded
Open this post in threaded view
|

Re: What is a reasonable amount of assembly code to implement gt, lt, eq?

tungsten
In reply to this post by cadet1620
It seems that using labels (to act like functions) as shown in the link doesn't cut down on the number of instructions needed to perform a gt, lt, or eq operations. Instead it cuts down on the number of times the same code is repeated in the program (leading to smaller file sizes). So when the computer is executing an operation, it will still need to go through the same number of instructions to obtain the desired result... the only difference is that it will have to jump to somewhere else in the program to get said instructions. If anything it increases the number of instructions as additional logic is needed to coordinate the jumps to and from.

Is this a correct understanding, or am I missing something?
Reply | Threaded
Open this post in threaded view
|

Re: What is a reasonable amount of assembly code to implement gt, lt, eq?

cadet1620
Administrator
You are completely correct. Assembly language subroutines shorten code size by eliminating duplicated code, but increase execution time due to the overhead of calling them.

// inline code// ASM call
k instr.@RIP$123
D=A
@ASM$FN
0;JMP
(RIP$123)
 
// ASM subroutine
(ASM$FN)
@R15
M=D
k instr.
@R15
A=M
0;JMP

If the VM code contains n instances for this instruction, then the total code size and execution times for the instructions is

Code size  Exec. time
Inlinenk nk
call4 n + k + 5 n(k + 9)

A bit of algebra will reveal that for 9 ≤ k ≤ 12 there must be a least 3 instances of the command to get size reduction.

 

Optimization is generally a trade off between code size and execution speed.

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: What is a reasonable amount of assembly code to implement gt, lt, eq?

tungsten
Thanks for the clarification! And also for the analysis.