Hi, I'm new to assembler, and I'm trying to make a malloc function for the hitachi h/800 cpu.
It's standard AT&T syntax, with a few modifications.
I've read the hard- and software manual, as well as tutorials, but there's still a few things I don't grasp.
input:
start_adr = Start address in memory
byte = byte value, which I want to overwrite the memory area with
size = number of bytes to overwrite
malloc(int* start_adr, int byte, int size){
__asm__ __volatile (
"add.w %0, %2"
"0: cmp.w %0, %2"
"beq 1f"
"mov.b %1. @%0"
"add.b %0, 1"
"bra 0b"
"1:"
);
};
line 1 of the assembler code, I add the size og bytes to change to the starting address. Thus getting the end address.
line 2, I compare to see if the starting adress is equal to the end address (if it is, we are done)
line 3, I branch out of the loop, since the right number of bytes have been overwritten
line 4, I move the byte value to the start_adr location, which is the current byte to manipulate
line 5, I add one (1) to the address, to move to the next byte
line 6, I loop back to label 0
Now, the problem is about wheter to use word or byte (mov.b or mov.w). I want to move one byte at a time, so naturally it should be mov.b - but should it be high or low byte? And how do I get the next byte? Another cmp statement?
If you're only ever pulling out one byte at a time, and only increasing the pointer by one each time, then there aren't any issue with byte-ordering.
You only have to worry about that if you read a word at a time, and then write a byte at a time.
However, if you read a word, then write a word, then add 2 to the pointer - it will still work fine; the internal read/write mechanism will sort out any byte ordering worries since they both use the same native format :wink
make it work a byte at a time, then later change it.
btw - why move bytes at all ? malloc does not usually clear memory.
Yeah, sorry about that.. The function is memset :D
And yes, it will do it one byte at a time.
Here is some example code for the x86. Hopefully this will help.
I did not write it, just modified it. Other here wrote it but I cant find that link right now.
align 4
memory_clear proc uses edi lpMem:DWORD, numBytes:DWORD, bUserVal:BYTE
mov edi,lpMem ; Pointer to memory
mov dl,bUserVal ; Value to initialize memory with
xor eax,eax
.if dl != 0 ; Build dword
mov al,dl
shl eax,8
mov al,dl
mov cx,ax
shl eax,16
mov ax,cx
.endif
mov ecx,numBytes ; number of bytes to write
push ecx
shr ecx,2 ; numbers of dwords to write
rep stosd
pop ecx
and ecx,3 ; number of remaining bytes to write
rep stosb
xor eax,eax
ret
memory_clear endp
This does some dwords at a time but then the remaining bytes are done.
You CAN do all bytes a byte at a time. Note: a BYTE at a time.