如何在汇编中使用LODSB?

5

如何使用LODSB来加载相对地址到我的代码中的字符串?最少需要哪些步骤?

我有一个测试程序,我正在使用PXE引导它。我有两种引导方式:通过pxelinux.0和直接引导。如果我直接引导,我的程序会打印两个字符串。如果我通过pxelinux.0引导,它只会打印第一个字符串。

为什么?

答案:代码没问题,初始地址计算错误。请参见下文。

工作技术(适用于两种引导方式):

  • 将方向标志设置为增加,cld
  • ds设置为cs
  • 将字符串的地址(从开头)放入si
  • 将起始偏移量添加到si

非工作技术(仅适用于pxelinux):

  • 根据 (((cs << 4) + offset) >> 4) 计算新的段地址
  • ds 设置为该地址(可以是A000或07C0)

在这里输入文本以修复Markdown中的错误

// Note: If you try this code, don't forget to set 
//       the "#if 0" below appropriately!

    .text
    .globl  start, _start

start:  
_start: 
_start1:    

    .code16

    jmp real_start

    . = _start1 + 0x1fe
    .byte 0x55, 0xAA

    // Next sector
    . = _start1 + 0x200

    jmp real_start

test1_str:
    .asciz  "\r\nTest: 9020:fe00"
test2_str:
    .asciz  "\r\nTest: a000:0000"

real_start:

    cld         // Make sure %si gets incremented.

#if 0
    // When loaded by pxelinux, we're here:
    // 9020:fe00 ==> a000:0000

    // This works.
    movw    $0x9020, %bx
    movw    %bx, %ds
    movw    $(test1_str - _start1), %si
    addw    $0xfe00, %si
    call    print_message

    // This does not.
    movw    $0xA000, %bx
    movw    %bx, %ds
    movw    $(test2_str - _start1), %si
    call    print_message
#else
    // If we are loaded directly without pxelinux, we're here:
    // 0000:7c00 ==> 07c0:0000

    // This works.
    movw    $0x0000, %bx
    movw    %bx, %ds
    movw    $(test1_str - _start1), %si
    addw    $0x7c00, %si
    call    print_message

    // This does, too.
    movw    $0x07c0, %bx
    movw    %bx, %ds
    movw    $(test2_str - _start1), %si
    call    print_message
#endif

    // Hang the computer
    sti
1:
    jmp 1b


// Prints string DS:SI (modifies AX BX SI)
print_message:
    pushw   %ax
    jmp 2f
3:
    movb    $0x0e, %ah  /* print char in AL */
    int $0x10       /* via TTY mode */
2:  
    lodsb   (%si), %al  /* get token */
    cmpb    $0, %al     /* end of string? */
    jne 3b
    popw    %ax
    ret

.balign 0x200

这是编译结果:
/usr/bin/ccache gcc -Os -fno-stack-protector -fno-builtin -nostdinc  -DSUPPORT_SERIAL=1 -DSUPPORT_HERCULES=1 -DSUPPORT_GRAPHICS=1 -DHAVE_CONFIG_H -I. -Wall -ggdb3 -Wmissing-prototypes -Wunused -Wshadow -Wpointer-arith -falign-jumps=1 -falign-loops=1 -falign-functions=1 -Wundef -g -c -o ds_teststart_exec-ds_teststart.o ds_test.S
/usr/bin/ccache gcc  -g   -o ds_teststart.exec -nostdlib -Wl,-N -Wl,-Ttext -Wl,8000 ds_teststart_exec-ds_teststart.o  
objcopy -O binary ds_teststart.exec ds_teststart

如果问题最终证明是完全不同的东西,我会重新用不同的措辞提出问题以便得到答案。 - Harvey
2个回答

4

第一个问题:

9020:FE00 ==> 9000:0000 而不是 A000:0000
我在使用 grldrstart.S 中的一些代码来进行该计算。grldrstart.S 程序中存在一个错误。它将 FE00 偏移量右移了 4 位,但是没有保留符号;FE00 是一个负数。所以正确的结果应该是:

shrw   $4, %bx

它应该已经

sarw   $4, %bx   // Preserves sign!!

90200 + FE00 = 90200 - 200 = 90000

问题答案:

为了使用LODSB,你必须:

  • 正确设置ds(并使用正确的数学运算)
  • 正确设置方向标志位,使用cldstd进行递增和递减
  • si设置为源缓冲区的偏移量。
  • 读取的有效地址将为(ds << 4) + si

0

设置 ds -->

压入 cs

弹出 ds


@smiffy: 但我想计算一个新的段地址,这样我就不必为每个基于ds:si的指令添加原始偏移量。因此,在我的程序中,位于偏移量0x40处的字符串可以是(0000 << 4) + 7c00 + 40(07c0 << 4) + 40 - Harvey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接