Author Topic: Clear allocated resb memory block  (Read 13773 times)

Offline coder

  • Jr. Member
  • *
  • Posts: 3
Clear allocated resb memory block
« on: June 02, 2013, 02:56:00 PM »
Good day, tell me a way to clear the memory block allocated as resb?  ???

Code: [Select]
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



Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Clear allocated resb memory block
« Reply #1 on: June 02, 2013, 04:53:13 PM »
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


Offline coder

  • Jr. Member
  • *
  • Posts: 3
Re: Clear allocated resb memory block
« Reply #2 on: June 03, 2013, 04:05:02 AM »
Tnx for help, but im listing full code my programm for point locate where need clear allocated memory block.

Code: [Select]
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
Code: [Select]
./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
Code: [Select]
./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...

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: Clear allocated resb memory block
« Reply #3 on: June 03, 2013, 05:36:15 AM »
What you are doing is not wrong, but overly complicated and hard to follow.  Here is a simplified version of your program.

Code: [Select]
                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.
Code: [Select]
        _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

Offline coder

  • Jr. Member
  • *
  • Posts: 3
Re: Clear allocated resb memory block
« Reply #4 on: June 03, 2013, 04:27:59 PM »
thanks a lot
Excellent example.