lullaby2005
lullaby2005

Reputation: 21

What's the difference of those 2 assembly code segment?

I'm learning to write a boot loader for X86 machine using tool "as" and "ld", but I meet a question.

The makefile is as following:

AS=as
LD=ld

.s.o:
        ${AS} -a $< -o $*.o >$*.map

all: final.img

final.img: bootsect
        mv bootsect final.img

bootsect: bootsect.o
        ${LD} --oformat binary -N -e start -Ttext 0x7c00 -o bootsect $<

clean:
    rm -rf *.o *.map *.img

When I make the file bootsect.s below to build final.img, there's no problem and yes "Hello world" shows on the screen

    .text
    .globl start
    .code16
start:
    jmp code
msg:
    .string "Hello world!\x0"
code:
    movw $0xb800, %ax
    movw %ax, %es #for display buffer address
        movw $0, %ax
    movw %ax, %ds   
    movw $msg, %si
    movw $0, %di
    cld
    movb $0x07, %al
1:
    cmp $0, (%si)
    je 1f
    movsb #DS:(E)SI to ES:(E)DI
    stosb #AL to ES:(E)DI
    jmp 1b
1:      jmp 1b
.org 0x1fe, 0x90
.word 0xaa55

But if I have some little change of the bootsect.s above as following:

    .text
    .globl start
    .code16
msg:
    .string "Hello world!\x0"
start:
    movw $0xb800, %ax
    movw %ax, %es #for display buffer address
    movw $0, %ax
    movw %ax, %ds   
    movw $msg, %si
    movw $0, %di
    cld
    movb $0x07, %al
1:
    cmp $0, (%si)
    je 1f
    movsb #DS:(E)SI to ES:(E)DI
    stosb #AL to ES:(E)DI
    jmp 1b
1:      jmp 1b
.org 0x1fe, 0x90
.word 0xaa55

There's nothing output from screen.

I'm confused why ? Could you help me ?

Thanks.

Upvotes: 2

Views: 317

Answers (1)

ughoavgfhw
ughoavgfhw

Reputation: 39905

When you output a binary file, the entry point you specify makes no difference to the running of the program since there is no header to specify it in. The loader simply starts at a certain offset, which with bootloaders is the beginning of the file. When you put the string as the first thing in your assembly, it will also be the first thing in your output, which means the processor will treat it as code and try to execute it. If you're lucky, the string will generate an invalid opcode quickly, allowing you to determine what happened. If you're unlucky, the string will be valid code and the program will "run", but the results will not be what you expect.

Upvotes: 4

Related Questions