assemblyx86instructionsas86

Linux kernel 0.01 bootloader used rep movw, not rep movsw? Does that actually repeat plain MOV?


I am new to assembly and I am trying to understand Linux 0.01 bootloader code but I got stuck at this part (at very beginning :) ):

.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text

BOOTSEG = 0x07c0
INITSEG = 0x9000
SYSSEG  = 0x1000            | system loaded at 0x10000 (65536).
ENDSEG  = SYSSEG + SYSSIZE

entry start
start:
    mov ax,#BOOTSEG
    mov ds,ax
    mov ax,#INITSEG
    mov es,ax
    mov cx,#256
    sub si,si
    sub di,di
    rep
    movw
    jmpi    go,INITSEG

This code (as explained in source comments) copies the bootloader to a new location and continues execution from go.

rep followed by movs should do this (copying part), but instead the instruction mov{w} is used.

rep
movw

In every reference book for x86 I looked rep is used with string instructions. Same for Intel's manual entry for the prefix.

Can rep be used with all data transfer instructions or just string ones, and if so why is it not mentioned in reference manuals?


Solution

  • This is most likely just the way the particular assembler that the code is written for implements this instruction though it is admittedly odd. It is more common to find this as movsb, movsw or movsl since movs is the notation from Intel.

    This is, in fact, moving a "string", though it is simply a set of arbitrary bytes. More specifically, you may have noticed mov cx,256 preceding this. This opcode is movw which will move a 16 bit word, more efficiently moving the 512 byte boot sector.

    Update:

    Your question made me awfully curious since I have not run across the instruction encoded in this way. Interestingly enough I found a forum entry that seems to indicate that rep movw is AS86 format and does in fact assemble to rep movsw.