assembly .set directive gives error invalid operands (.data and *UND* sections)

旧城冷巷雨未停 提交于 2019-12-12 02:35:35

问题


I am learning to write a bootloader. As a part of experiment, I want to be able to print hexadecimal values as strings. I wrote following assembly code which doesn't entirely implement hex to string functionality. However, I expected following code to at least assemble correctly.

$ cat print_bios.S 
.file "print_bios.S"

.section .data
.hex_str:
    .ascii "xxxxxxxxxxxxxxxx"
    .set hex_size, .-hex_str

.section .text
    .global .hex_to_string

hex_to_string:
    push %rbp
    mov  %rsp, %rbp

    /*
     * leave speace for local variables
     *
     * 8 byte counter        count   -8
     * 8 byte temp memory    byte    -16
     * 8 byte to store 0xf   tmp     -24
     *
     * Variables on stack
     * 8 first argument      hex_number +8
     * 8 pointer to string   hex_string +16
     * 8 size of string      size       +24
     */

    sub $0x24, %rsp

    /* find number of characters required for string */

    movq 8(%rbp), %rax    /* ax = hex_number */
    movq $0, %rcx
    jmp .L2

.L1:
    shrq $4, %rax          /* rax >>= 4 */
    addq $1, %rcx          /* rcx++ */
.L2:
    testq %rax, %rax       /* while (rax) */
    jne .L1

    /* if (count > size) { return -1; } */
    movq %rcx, -0x4(%rbp)
    cmpq %rcx, 0x12(%rbp)
    jg .ERROR

.out:
    movq $0, %rax
    ret
.ERROR:
    movq $-1, %rax
    ret

    .global _start
_start:
    pushq $0x56A
    pushq hex_string
    pushq hex_size
    call hex_to_string

Assembling above code fails with following error

$ as -o print_bios.o print_bios.S 
print_bios.S: Assembler messages:
print_bios.S: Error: invalid operands (.data and *UND* sections) for `-' when setting `hex_size'

I am not sure I understand the reason assembling fails. It would be good if someone can help me.

来源:https://stackoverflow.com/questions/30036738/assembly-set-directive-gives-error-invalid-operands-data-and-und-sections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!