New answers tagged

0

As was helpfully pointed out by G. Sliepen in a comment, you can flip the bottom bit using xor. That enables the above code to be rewritten in one fewer instruction: isEven: andi $t0, $a0, 1 xori $v0, $t0, 1 jr $ra This takes advantages of the properties: A xor 1 = !A A xor 0 = A


2

Sorry for the poor code formatting, looks like 8-width tabs didn't carry over so well. A well known problem on StackExchange... I looked at your text in a separate editor and can vouch that your indentations are fine except on the lines that define the labels deci, addts, and sumtm. Best practices for naming? Is there a common length limit for labels? (...


1

Code style Your code style is certainly acceptable - everyone is entitled to their own coding habits. To further improve readability, and readability is key, I would change the following: put all labels in the same separate column. You didn't do this for is_even, fmt, target, list, and len. don't just have 1 space character between the instruction/directive ...


3

C++17 introduces std::clamp(), so your function can be implemented as follows: #include <algorithm> inline BYTE Clamp(int n) { return std::clamp(n, 0, 255); } Which seems well optimized by GCC (version 10.2), using only comparison and conditional move instructions as seen in many of the older answers: clamp(int): cmp edi, 255 ...


5

Multiplying by 3 The multiplication part seems overly complicated. The problem is, mul doesn't take an immediate, and the register that I want to multiply is ebx, not eax, so I need to do a couple movs before I can multiply. This is all true, but based on the premise that the mul instruction must be used. Here are a couple of alternatives: imul ebx, ebx, ...


Top 50 recent answers are included