NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: coder on June 02, 2013, 02:56:00 PM
-
Good day, tell me a way to clear the memory block allocated as resb? ???
global _start
section .bss
buffer resb 256
readsym resb 1
section .data
count db 0
section .text
_start:
mov edi,buffer
mov bl,256
nsym: call read
mov al,[readsym]
cmp al,0xa
je enter
mov [edi],al
cmp bl,0
je full
inc bl
inc edi
mov [count],bl
jmp nsym
enter: mov ecx,buffer
mov edx,[count]
call view
exit: mov eax,4
xor ebx,ebx
int 0x80
read: pushad
mov eax,3
mov ebx,1
mov ecx,readsym
mov edx,1
int 0x80
popad
ret
view: pushad
mov eax,4
mov ebx,1
int 0x80
popad
ret
-
Despite the fact that we call it "uninitialized" memory, the loader in fact clears it (except in a dos .com file). To clear it for re-use, "rep stosd" should work (see http://www.agner.org for optimization tips). It may be enough to simply reset your pointer and "count". Does it really need to be cleared?
You have some other problems... You define "count" as a byte, but use it as a dword. 256 won't fit in bl. Nasm warns about this, but will assemble it anyway. bl is in fact zero to begin with. After calling "view", you fall through into "exit:" - which is sys_write, not sys_exit! In "read", you've got ebx=1 which is stdout, stdin is 0 - Unix doesn't seem to care, it'll work anyway. You jump to "enter:" before updating "count", which may be a problem...
If you have trouble straightening this out, we're here to help!
Best,
Frank
-
Tnx for help, but im listing full code my programm for point locate where need clear allocated memory block.
lobal _start
section .bss
buffer resb 256
insym resb 1
outsym resb 1
part1 resb 1
part2 resb 1
readsym resb 1
count resb 1
section .data
map db '0123456789ABCDEF'
section .text
_start:
mov edi,buffer
mov bl,0
next: call read
mov al,[readsym]
cmp al,0xa
je enter
mov al,[readsym]
mov [edi],al
inc edi
cmp bl,0xff
je exit
inc bl
mov [count],bl
jmp next
enter: ; THERE NEED CLEAR TOTAL BUFFER
mov edi,buffer
mov ecx, [count] ;count syms
nsym:
mov al,[edi]
mov [insym],al
call symtohex2
inc edi
loop nsym
jmp next
exit: mov eax,1
xor ebx,ebx
int 0x80
read: pushad
mov eax,3
mov ebx,0
mov ecx,readsym
mov edx,1
int 0x80
popad
ret
view: pushad
mov eax,4
mov ebx,1
int 0x80
popad
ret
viewsym: pushad
mov eax,4
mov ebx,1
mov ecx,outsym
mov edx,1
int 0x80
popad
ret
symtohex: pushad
mov ah,[insym] ; 0010.1010 (2.a)
mov al,[insym] ; 0010.1010 (2.a)
shl ah,4 ; 1010.0000 = ah
shr ah,4 ; 0000.1010 = ah = 10 bingo lefst side found = ah
shr al,4 ; 0000.0010 = al = 2 bingo 2h
mov ebx,map
xlat ; al(2) => 2 ascii from map
mov [part1],al
mov al,ah
xlat
mov [part2],al
mov ah,[part1]
mov [outsym],ah
call viewsym
mov ah,[part2]
mov [outsym],ah
call viewsym
popad
ret
symtohex2: pushad
mov ebx,map
mov ah,[insym]
mov al,[insym]
and al,11110000b ; part1
and ah,00001111b ; part2
shr al,4
xlat ; al = map(al)
mov [outsym],al
call viewsym
mov al,ah
xlat
mov [outsym],al
call viewsym
mov al,' '
mov [outsym],al
call viewsym
popad
ret
get
./str_to_hex
tester program
74 65 73 74 65 72 20 70 72 6F 67 72 61 6D nasm
74 65 73 74 65 72 20 70 72 6F 67 72 61 6D 6E 61 73 6D
i want
./str_to_hex
tester program
74 65 73 74 65 72 20 70 72 6F 67 72 61 6D
nasm
6E 61 73 6D
and more...
-
What you are doing is not wrong, but overly complicated and hard to follow. Here is a simplified version of your program.
global _start
section .bss
buffer resb 256
Notice how input is simplified and read does not need to be a subroutine because it's only used once.
_start mov eax, 3
mov ecx, buffer
mov edx, 256
int 0x80
dec eax
jnz .Parse
inc eax
xor ebx, ebx
int 0x80
.Parse mov esi, ecx
mov ecx, eax
.Next lodsb
call Conv
loop .Next
; If you really insist of deleting buffer this is how you'd do it, but as Frank pointed out
; it's not necessary.
mov edi, buffer
mov ecx, 256
xor eax, eax
rep stosb
jmp _start
Trace through this in the debugger and you'll see how the clearing part isn't necessary. Your conversion from ASCII to HEX are overly complicated too, so if you'd like a hand with them I can show you an example. You've done a pretty good job for your experience level though
-
thanks a lot
Excellent example.