corrected MIN and MAX

This commit is contained in:
Perry Kivolowitz 2023-02-17 09:30:45 -06:00
parent ab115d6fce
commit 60c56840dd
11 changed files with 225 additions and 20 deletions

View file

@ -79,7 +79,13 @@ These resolve to: `.cfi_startproc` and `.cfi_endproc` respectively.
### MIN and MAX
Handy more readable macros for determining minima and maxima.
Handy more readable macros for determining minima and maxima. Note that
the macro performs a `cmp` which subtracts `src_b` from `src_a`
(discarding the results) in order to set the flags to be interpreted by
the following `csel`.
Thank you to u/TNorthover for nudge to add the cmp directly into the
macro.
Signature:

Binary file not shown.

View file

@ -1,3 +1,6 @@
/* A test program.
*/
#include "apple-linux-convergence.S"
.text
@ -23,7 +26,6 @@ MAIN
END_PROC
dbl: .double -0.55
flt: .float 0.125
fmt: .asciz "%f\n"
fmt: .asciz "Should print -0.550000: %f\n"
.end

View file

@ -1,3 +1,5 @@
/* A test program.
*/
#include "apple-linux-convergence.S"
.text
@ -24,6 +26,6 @@ MAIN
END_PROC
flt: .float 0.125
fmt: .asciz "%f\n"
fmt: .asciz "Should print 0.125000: %f\n"
.end

96
macros/minmax.S Normal file
View file

@ -0,0 +1,96 @@
/* A test program. Tests MIN and MAX
*/
#include "apple-linux-convergence.S"
.text
.align 2
GLABEL main
MAIN
START_PROC
PUSH_P x29, x30
PUSH_P x20, x21
mov x29, sp
// Generate two numbers to compare.
mov x0, xzr
CRT time // time(nullptr)
CRT srand
CRT rand
and x20, x0, 0xFF
CRT rand
and x21, x0, 0xFF
bl PrintLegend
bl DoMin
bl DoMax
POP_P x20, x21
POP_P x29, x30
mov w0, wzr
ret
END_PROC
DoMin:
START_PROC
PUSH_P x29, x30
mov x29, sp
LLD_ADDR x0, fmtls
MIN x20, x21, x1
#if defined(__APPLE__)
PUSH_R x1
CRT printf
add sp, sp, 16
#else
CRT printf
#endif
POP_P x29, x30
ret
END_PROC
DoMax:
START_PROC
PUSH_P x29, x30
mov x29, sp
LLD_ADDR x0, fmtgt
MAX x20, x21, x1
#if defined(__APPLE__)
PUSH_R x1
CRT printf
add sp, sp, 16
#else
CRT printf
#endif
POP_P x29, x30
ret
END_PROC
PrintLegend:
START_PROC
PUSH_P x29, x30
mov x29, sp
LLD_ADDR x0, fmt
#if defined(__APPLE__)
PUSH_P x20, x21
CRT printf
add sp, sp, 16
#else
mov x1, x20
mov x2, x21
CRT printf
#endif
POP_P x29, x30
ret
END_PROC
.p2align 2
fmt: .asciz "Values to compare: %d and %d\n"
.p2align 2
fmtgt: .asciz "Greater value is: %d\n"
.p2align 2
fmtls: .asciz "Lesser value is: %d\n"
.end

Binary file not shown.

View file

@ -119,10 +119,28 @@ main:
ldr \a, [sp], 16
.endm
.macro MIN src_a, src_b, dest
csel \dest, \src_a, \src_b, GT
.endm
/* The smaller of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
.macro MIN src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, LT
.endm
/* The larger of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, GT
.endm

View file

@ -119,10 +119,28 @@ main:
ldr \a, [sp], 16
.endm
.macro MIN src_a, src_b, dest
csel \dest, \src_a, \src_b, GT
.endm
/* The smaller of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
.macro MIN src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, LT
.endm
/* The larger of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, GT
.endm

View file

@ -119,10 +119,28 @@ main:
ldr \a, [sp], 16
.endm
.macro MIN src_a, src_b, dest
csel \dest, \src_a, \src_b, GT
.endm
/* The smaller of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
.macro MIN src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, LT
.endm
/* The larger of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, GT
.endm

27
section_1/regs/quinn.S Normal file
View file

@ -0,0 +1,27 @@
.p2align 2
.text
.global main
main: stp x29, x30, [sp, -16]!
str x20, [sp, -16]!
mov x29, sp
mov x20, xzr
1: cmp x20, 10
beq 2f
ldr x0, =fmt
mov x1, x20
bl printf
add x20, x20, 1
b 1b
2: ldr x20, [sp], 16
ldp x29, x30, [sp], 16
mov w0, wzr
ret
.data
fmt: .asciz "Number: %d\n"
.end

View file

@ -119,10 +119,28 @@ main:
ldr \a, [sp], 16
.endm
.macro MIN src_a, src_b, dest
csel \dest, \src_a, \src_b, GT
.endm
/* The smaller of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
.macro MIN src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, LT
.endm
/* The larger of src_a and src_b is put into dest. A cmp instruction
or other instruction that sets the flags must be performed first.
This macro makes it easy to remember which register does what in the
csel.
Thank you to u/TNorthover for nudge to add the cmp.
*/
.macro MAX src_a, src_b, dest
cmp \src_a, \src_b
csel \dest, \src_a, \src_b, GT
.endm