Assembly 8086 - copy one buffer to another

2020-06-28 04:46发布

问题:

im working on a assembly program that reads the whole text file into a buffer then displays it in the console. It displays 24 lines (each line has a max lenght of 80, because im using a 80 wide * 25 height dossbox ) at once then waits for user input so the user can scroll through the text.

I wanted to add the number of line to the beginning of each line, so figured i could make a second buffer and copy chars 1by1 from the first one and when i find a newline i would call a procedure which would add the line number to the buffer, then continue till i proceed thru the whole buffer. But my way of copying from one buffer to the other is bad.

So i wanna copy BUFFA to BUFFB:

mov di,OFFSET BUFFB ;so i set di to the beggining of bufferB




mov si,Pos         ;Pos is my position in the first buffer
lea bx,BUFFA[si]   ;move the content of buffA to bx , i think the problem is here
mov [di],bx        ;move to the addres of di the content of bx
inc di
inc Pos

the problem is when i print out the content of the second buffer i find out that i copy the value of si(same as Pos) to my buffer and not the content of the bufferA[si]. How can i fix this code?

Edit 1:

So the solution is using mov and al register:

mov si,Pos
mov al,[BUFF + si]
mov [di],al
inc di

回答1:

you can use

lodsb

instead of

mov al,[si]
inc si

and

stosb

instead of

mov [di],al
inc di

in best cases, you can combine both to

movsb    ; move byte at [si] to [di], and increase both indices

if you know how many bytes to copy, you can even move memory blocks using "rep", which repeats the instruction afterwards CX times:

cld                    ; make sure that movsb copies forward
mov si, source_buffer
mov di, dest_buffer
mov cx, #amount of bytes to copy
rep movsb

or fill memory blocks

cld                  ; make sure that stosb moves forward
mov si, buffer       ; start here
mov al, 0xFF         ; fill with 0xFF
mov cx, #20          ; 20 times
rep stosb

if you're using words instead of bytes, use lodsw, stosw and movsw

All these instructions can either go
- forward (INC si/di) when the direction flag is cleared (via CLD) or
- backwards (DEC si/di). when the direction flag is set (via STD)