Modul Beispiel-Methode 2
- Erstellen sie eine Methode wie im Ersten Beispiel mit dem Namen mylen.
;simple non-optimized all-memory-type string-length function _MyLen: mov _HB2,_HA2 ;copy 3. byte (memory type encoded in upper nibble) swap _HB2 ;swap the upper nibble with lower nibble andi _HB2,0x0f ;mask the lower nibble andi _HA2,0x0f ;mask the lower nibble from 3. byte of string address ;string address now in _HA0,_HA1 and _HA2 ;memory type now in _HB2 ;now check for valid string address (>0) clr _LA0 ;clear a temp register mov _LA0,_HA0 ;move the 1. byte of string address or _LA0,_HA1 or _LA0,_HA2 breq __MyLenReturn ;if address is zero, break and return with value 0 (_HA0 is zero) movw ZH:ZL,_HA1:_HA0 ;copy the 1. and 2. byte to Z-Pointer PushZ ;save the string address ;now just call the auto-Loadbyte-Function of the standard library (auto-increment!) ;the LoadByte-Function is located in MainObj/ooLoadByte of the standard library call _ooLoadByte ;param: ZH:ZL[:RAMPZ] = Address, _HB2 = memory type, result in _LA0 PopZ ;restore string address push _LA0 ;save value from LoadByte call _StringFreeTemp ;free a temporary sram string (auto skips others) pop _HA0 ;restore value to return register __MyLenReturn: ret
Testen sie die Methode. Diese Methode gibt die Länge eines Strings zurück, egal ob er im Arbeitsspeicher, Flash oder Eeprom liegt. Es sind also alle Varianten eines Strings als Parameter erlaubt, auch Ergebnisse eines String-Ausdrucks. Ein Ggf. erzeugter temporärer String im Arbeitsspeicher (z.B. aus einem Ausdruck) wird automatisch freigegeben.
avr.device = atmega328p avr.clock = 20000000 avr.stack = 64 uart0.baud = 19200 'baud rate uart0.Recv.enable 'enable receive uart0.Send.enable 'enable send #library "library/custom/mymodule.module" print "myAdd() = ";str(myAdd(23,42)) ' = 65 print "myLen() = ";str(myLen("Hello World")) ' = 11 halt()