Re: Buffers in Assembly (NASM)




"bwaichu@xxxxxxxxx" <spamtrap@xxxxxxxxxx> wrote in message
news:9eb0b7cb-2b47-4e28-aad8-0cdc3a6b5a77@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm trying to better understand data structures in assembly. I know I
can create a zero filled buffer in the bss section in NASM with this:

buffer: times 64 db 0

I am unfamiliar with NASM and I think it also depends on the object
format you are using, but even if you put a zero as in above, doesn't
the bss section still need to be zero'd? The above line will only
occupy 64 bytes, it won't set it to zero. Am I correct?

And I know I can create a buffer on the stack like this:

sub esp, 64
mov ebx, esp ;save the start point of the buffer

But how do I zero out the buffer on the stack? In C, I would just do
something like:

char buffer[64] = {0};

Use your favorite C Compiler and compile this. Then either tell
the compiler to output Assembly code, or disassemble it yourself
and see what the compiler did.

What's the equivalent in assembly using NASM?

There are many ways. (I don't use NASM, but you should
be able to convert it easy enough)

1.
mov ecx,((64+3)>>2)
xor eax,eax
@@: push eax
dec ecx
jnz short @b

2.
sub esp,((64+3) & ~3)
xor eax,eax
...
mov [esp+n],eax
mov [esp+n+4],eax
mov [esp+n+8],eax
...

3.
sub esp,((64+3) & ~3)

mov ecx,((64+3)>>2)
xor eax,eax
mov ebp,esp
@@: mov [ebp+n],eax
add ebp,4
dec ecx
jnz short @b

4.
sub esp,((64+3) & ~3)
mov ebx,esp
push byte 64
push byte 0
push ebx
call memset ; assuming your DS == SS

5.
your favorite code goes here

Ben

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Forever Young Software
http://www.frontiernet.net/~fys/index.htm
To reply by email, please remove the zzzzzz's

Batteries not included, some assembly required.

.



Relevant Pages

  • Re: which book to start with...?
    ... mov eax, 4 ... just installed nasm 16 bit and 32 bit bins under dosemu. ... .bss wont accept initialisations while .data will but no garantee for modification at runtime. ... Section .bss is nominally "uninitialized" data, but is in fact cleard to zero. ...
    (alt.lang.asm)
  • Re: which book to start with...?
    ... mov eax, 4 ... buffer resb 1000h ... Section .bss is nominally "uninitialized" data, but is in fact cleard to zero. ... i used nasm's one because intel's manuals are too big and detailed for me yet:) also i hate pdf files... ...
    (alt.lang.asm)
  • Re: Buffers in Assembly (NASM)
    ... can create a zero filled buffer in the bss section in NASM with this: ... it doesn't know what ".bss" means. ... In an uninitialized section, there's "nothing there", so it would be "conceptually impossible" for Nasm to zero it. ... mov al, 'N' ...
    (comp.lang.asm.x86)
  • Re: debug and the newbe
    ... 0DF1:0102 mov dx, 107 ... 0DF1:0105 int 21 ... Here is a more appropriate example, written in NASM. ... AL is set to zero and that returns a code of ...
    (alt.lang.asm)
  • Re: 8031 question
    ... AUXBUF EQU 0; TRICK - BAFER ... BUFFER EQU 100H; PREPARE FOR OUTPUT ... MOV R0,#TEMPDIV; TEMPERATURU CITA SAMO U KRUGU 1 ... JMP EQSEC; ...
    (sci.electronics.design)