I am creating the following eZ80 assembly function, callable from a C program:
Code:
Here is the code for _chkbit:
Code:
And the main.c:
Code:
My problem is that the assembly function is not altering the value of byte, making it return the same value every time the program is run. How do I correct this?
On a side note, I am sure that this is probably nowhere near the most efficient algorithm for doing these functions. I would greatly appreciate any tips to make these two functions more efficient
EDIT: @Beckadam: I replaced the code in _chkbit with your code:
Code:
It doesn't work for some reason, however. I am still not quite sure how the ld (smc), a works. Could you explain, please?
Code:
public _setbit
_setbit:
;----------------------------------
; Sets pos bit of byte if not set; resets bit if set
; Arguments:
; arg0 = uint8_t *byte
; arg1 = uint8_t pos
; Returns:
; True if bit set; false, if bit reset
call _chkbit ; Returns state of bit in a
pop hl
pop de ; de = byte pointer
pop bc ; bc = pos
push bc
push de
push hl
ld b, c
inc b
ld c, 1
.shift_left:
sla c
djnz .shift_left ; After this loop completes the bit in e
; will be at pos
xor a, 0
jr z, .setbit
ld a, (de) ; Load byte into a
sub a, c ; If bit is set, reset it
ld (de), a ; Load the changed byte back into the original addr
xor a, a
ret
.setbit:
scf ; dbg_Debugger();
sbc hl, hl
ld (hl), 2
ld a, (de)
add a, c
ld (de), a
scf ; dbg_Debugger();
sbc hl, hl
ld (hl), 2
ld a, 1
ret
Here is the code for _chkbit:
Code:
public _chkbit
_chkbit:
;--------------------------------------------------
; Checks the pos bit of byte
; Arguments:
; arg0 = uint8_t byte
; arg1 = uint8_t pos
; Returns:
; True if bit set; false, otherwise
pop hl ; hl = stack pointer
pop de ; de = byte
pop bc ; bc = pos
push bc
push de
push hl
ld b, c ; Load pos into b
inc b
.shift_right:
srl e
djnz .shift_right
jr nc, .false
ld a, 1
ret
.false:
xor a, a
ret
And the main.c:
Code:
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <debug.h>
bool chkbit(uint8_t byte, uint8_t pos);
bool setbit(uint8_t *byte, uint8_t pos);
void main(void) {
uint8_t byte = 1;
uint8_t pos = 1;
dbg_sprintf(dbgout, "chkbit returns %d\n", chkbit(byte, pos));
dbg_sprintf(dbgout, "setbit returns %d\nbyte = %d", setbit(&byte, pos), byte);
return;
}
My problem is that the assembly function is not altering the value of byte, making it return the same value every time the program is run. How do I correct this?
On a side note, I am sure that this is probably nowhere near the most efficient algorithm for doing these functions. I would greatly appreciate any tips to make these two functions more efficient
EDIT: @Beckadam: I replaced the code in _chkbit with your code:
Code:
pop hl ; hl = stack pointer
pop bc ; c = byte
pop de ; e = pos
push de
push bc
push hl
ld a, e ; Load pos into a
rla
rla
rla
add a, $C1
ld (smc), a
set 0, c
smc:=$-1
ret
It doesn't work for some reason, however. I am still not quite sure how the ld (smc), a works. Could you explain, please?