Author Topic: REP MOVSB?  (Read 28681 times)

Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
REP MOVSB?
« on: July 04, 2011, 09:03:31 AM »
Hi,

Something hit me, I've never much used REP MOVSB but bumped into it today.
There's direction flag, but having it cleared (CLD) and doing a REP MOVSB

is there a way to move exactly 65536 bytes instead of 65535 bytes?
CX = cannot symbolize 64KB as it skips instruction alltogether when CX = 0.

Have I missed seomthing or is the maximum to move 65535 bytes?
no problem really, just got an itch that its not an even 64KB number

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: REP MOVSB?
« Reply #1 on: July 04, 2011, 02:53:09 PM »
Good question! The "loop" instruction, with cx=0, will do the full 64k iterations. Not so with the "rep" prefix. I think the way I would do it is:

Code: [Select]
; set up ds and es
xor si, si
xor di, di
mov cx, 65535
rep movsb
movsb  ; mov last byte

You might be able to get "rep" to use ecx (I'm assuming we're in 16-bit mode) with an "a32" (67h) override prefix...

Code: [Select]
; set up ds and es
xor esi, esi
xor edi, edi
mov ecx, 65536
a32 rep movsb

I'm not actually sure that would work. Are you in a position to try it (easily)? I'm not...

(really appreciate your feedback on the "align" situation, BTW!)

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: REP MOVSB?
« Reply #2 on: July 04, 2011, 06:05:26 PM »
I would opt for Frank's first example... keep it simple.

Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
Re: REP MOVSB?
« Reply #3 on: July 04, 2011, 07:18:24 PM »
Hey, great glad the feedback was appriciated, quick fix by you guys, great work on your side!

About the MOVSB I did some test actually as I found these instructions to me messy but curious. Honestly they are probably soon to be forgotten. Thinking at some point imagine engineers really put their minds into making these. Guess they had low priority on these rather unneeded instructions and did not bother having them "perfected". Thats my feeling about it anyway.

There was no go for me on the a32 rep movsb it did not even execute. I read somewhere about using A32 / 67h  so I find that kind of odd. I simply could not get it to run though so I had to leave that.

A MOV CX,16384 and MOVSD could very well probably be the best way solving it. Together with a STD allowing copying a lower memory into a higher memory when regions overlaps without anyproblem.

; MOVE 64KB (WITH OVERLAP) Using legacy MOVS instructions
;
; GROWING RAM - >
;
; |SOURCE|
;     |DESTINATION|
;
STD
MOV AX,SRCseg
MOV DS,AX
XOR SI,SI
MOV AX,DSTseg
MOV ES,AX
XOR DI,DI
MOV CX,16384
REP MOVSD

So that came to be my solution if using these instructions. :)

Have a great one!

Good question! The "loop" instruction, with cx=0, will do the full 64k iterations. Not so with the "rep" prefix. I think the way I would do it is:

Code: [Select]
; set up ds and es
xor si, si
xor di, di
mov cx, 65535
rep movsb
movsb  ; mov last byte

You might be able to get "rep" to use ecx (I'm assuming we're in 16-bit mode) with an "a32" (67h) override prefix...

Code: [Select]
; set up ds and es
xor esi, esi
xor edi, edi
mov ecx, 65536
a32 rep movsb

I'm not actually sure that would work. Are you in a position to try it (easily)? I'm not...

(really appreciate your feedback on the "align" situation, BTW!)

Best,
Frank
« Last Edit: July 04, 2011, 07:35:37 PM by dreamaco »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: REP MOVSB?
« Reply #4 on: July 04, 2011, 09:37:43 PM »
Important addition!

Code: [Select]
cld

It is considered polite to set the direction flag back "up" when you're done with it. Other than that, good solution.

As to the "why" of these instructions, in the day that they were invented (would have been only movsb and movsw), it typically took longer to fetch an instruction from memory than to execute it. So it was a performance "win" to have short code - and these instructions are short.

Can I come up with a complete list of the "string" instructions off the top of my head? lodsb, stosb, movsb, cmpsb, scasb, insb, outsb - and their "w", "d" and "q" versions... I think that's all of 'em.(?)

If you put the word "code" inside of square brackets, "[]" (like a Nasm memory reference) at the beginning of your code, and "/code" in "[]"s at the end of your code, it'll make "code blocks". I don't care, myself, but some of the moderators request that you use 'em!

Best,
Frank