I am creating the following eZ80 assembly function, callable from a C program:

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 Smile

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?
There's no sequence point. Move the setbit function outside of the dbg_sprintf call. The compiler pushes the current value of byte, calls the setbit function, pushes the return value, pushes some other stuff, calls dbg_sprintf, and returns.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement