Question

I compile a code.

.data
ssttrr:
.string "%d\n"
.text
.globl main
main:
mov $213, %rdx
push %rdx
push $ssttrr
call _printf
add  $8, %rsp

or it

.global main

    .text
main:

push    %rax                    # caller-save register
push    %rcx                    # caller-save register

mov     $format, %rdi           # set 1st parameter (format)
mov     %rax, %rsi              # set 2nd parameter (current_number)
xor     %rax, %rax              # because printf is varargs
call    printf                  # printf(format, current_number)

pop     %rcx                    # restore caller-save register
pop     %rax                    # restore caller-save register

mov     %rax, %rdx              # save the current number
mov     %rbx, %rax              # next number is now current
add     %rdx, %rbx              # get the new next number
dec     %ecx                    # count down
jnz     print                   # if not done counting, do some more

pop     %rbx                    # restore rbx before returning


mov  $60, %rax
xor  %rdi, %rdi
syscall

format:
    .asciz  "%20ld\n"

why the "format" is in a .text field?

gcc -nostdlib file.s -o program

Why do I get an error?

/universe/earth/asm/file.s:14: undefined reference to `_printf' collect2: ld returned 1 exit status

how to fix this error?

if so then all the code works. Why???

.global main

    .text
main:
    push    %rbx                    # we have to save this since we use it

    mov     $90, %ecx               # ecx will countdown to 0
    xor     %rax, %rax              # rax will hold the current number
    xor     %rbx, %rbx              # rbx will hold the next number
    inc     %rbx                    # rbx is originally 1
print:
    # We need to call printf, but we are using eax, ebx, and ecx.  printf
    # may destroy eax and ecx so we will save these before the call and
    # restore them afterwards.

    push    %rax                    # caller-save register
    push    %rcx                    # caller-save register

    mov     $format, %rdi           # set 1st parameter (format)
    mov     %rax, %rsi              # set 2nd parameter (current_number)
    xor     %rax, %rax              # because printf is varargs
    call    printf                  # printf(format, current_number)

    pop     %rcx                    # restore caller-save register
    pop     %rax                    # restore caller-save register

    mov     %rax, %rdx              # save the current number
    mov     %rbx, %rax              # next number is now current
    add     %rdx, %rbx              # get the new next number
    dec     %ecx                    # count down
    jnz     print                   # if not done counting, do some more

    pop     %rbx                    # restore rbx before returning
    ret
 format:
    .asciz  "%20ld\n"

Why the function print: isn't called in main field?

cpy: intel-64 os: debian

Was it helpful?

Solution

why the "format" is in a .text field?

.text in assembly means "code section"

gcc -nostdlib file.s -o program Why do I get an error? /universe/earth/asm/file.s:14: undefined reference to `_printf' collect2: ld returned 1 exit status

you are not linking the standard libraries, where printf is located (-nostdlib option): the linker cannot find it

how to fix this error?

Either link the standard library (stdlib) explicitly, or link another library that provides a printf implementation, or remove the -nostdlib switch

Why the function print: isn't called in main field?

"print" is just a label: you can call it, jmp to it, or just let the control flow of the program go into it, as in this case. (I can tell that print is not a "function" because it lacks a proper prologue).

May I suggest you some assembly introductions?

Or many others you can find on the internet

OTHER TIPS

why the "format" is in a .text field? It's unclear what you're asking here, but I think you're asking about two things, compressed here:

mov     $format, %rdi           # set 1st parameter (format)

format:
    .asciz  "%20ld\n"

In your .text section, you're refering to the format label. Later, you define the format label.

Why do I get an error? You're specifying the compiler option -nostdlib which excludes the libraries that normally get included, including the printf function.

Why the function print: isn't called in main field? There isn't a function print, but there is a label, and it is being used:

jnz     print                   # if not done counting, do some more

It appears to be the termination point of a loop based on %ecx, which counts from 0x90 to 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top