How do I clear a buffer?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mofule
Posts: 12
Joined: Wed Sep 27, 2017 2:50 pm

How do I clear a buffer?

Post by mofule »

Sup.

Once again, I have a problem. I know this is stupid but how do I clear a buffer? I've made my second stage into a shell. It stores every character into ES:DI (pointer to buffer) and compares it.

I want to clear it. How would I do that? So there's nothing in the buffer. This is how I made it.

Code: Select all

buffer times 64 db 0
Computers aren't to blame... the ones who program them are.
TheZeus121
Posts: 2
Joined: Sat Sep 30, 2017 7:37 am

Re: How do I clear a buffer?

Post by TheZeus121 »

Hi,
At the start the buffer is already empty with zeroes, but if you want to clear it again, I would do it like this:
(supposing that es:di already points to the buffer)

Code: Select all

mov cx, 64      ; the length of buffer
mov al, 0       ; put zeroes everywhere
clear_loop:
stosb           ; this will put byte stored in al to the address [es:di] and increase di
loop clear_loop ; if cx is not zero, loop back and decrease cx
if you are in 32 bit mode replace cx with ecx, if in 64, then with rcx.

Note: this isn't the most effective, you could use stosw or even stosd or stosq, but this is the easiest to explain and understand IMO.

hope this helps :D
Octocontrabass
Member
Member
Posts: 5586
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I clear a buffer?

Post by Octocontrabass »

LOOP? Why not REP?

Code: Select all

mov cx, 64
mov al, 0
rep stosb
User avatar
CorruptedByCPU
Member
Member
Posts: 79
Joined: Tue Feb 11, 2014 4:59 pm

Re: How do I clear a buffer?

Post by CorruptedByCPU »

Octocontrabass wrote:LOOP? Why not REP?

Code: Select all

mov cx, 64
mov al, 0
rep stosb
Ha ha, and where is DI/EDI/RDI? ;)

Code: Select all

; nasm
xor al, al
mov cx, 0x40
mov di, buffer
rep stosb
https://blackdev.org/ - system programming, my own 64 bit kernel and software.
Octocontrabass
Member
Member
Posts: 5586
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I clear a buffer?

Post by Octocontrabass »

akasei wrote:Ha ha, and where is DI/EDI/RDI? ;)
TheZeus121 wrote:(supposing that es:di already points to the buffer)
;)
Post Reply