2

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
6
  • 1
    lea (load extended address) loads the address of the argument, not the content. You actually want a mov here. Commented Mar 21, 2016 at 22:39
  • ty for your fast answer, but mov bx, offset BUFFB[si] is not legal operation
    – Shaoe
    Commented Mar 21, 2016 at 22:42
  • Remove the offset part. Commented Mar 21, 2016 at 23:00
  • Ty. Also i have to use al register and remove the offset part.
    – Shaoe
    Commented Mar 21, 2016 at 23:06
  • 2
    @500-InternalServerError just for the record, it's load effective address, not extended.
    – Jester
    Commented Mar 21, 2016 at 23:29

1 Answer 1

10

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)

2
  • 1
    You might improve your answer by including the CLD instruction. It's all-important with string primitives and all too often neglected.
    – Fifoernik
    Commented Mar 24, 2016 at 11:29
  • Thanks. I also edited your answer to correct a few mistakes (probably due to haste?) Upvoted!
    – Fifoernik
    Commented Mar 24, 2016 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.