G
G
Green Chameleon2020-01-29 01:32:09
assembler
Green Chameleon, 2020-01-29 01:32:09

How to enter and display a number in assembler(AT&T, GAS syntax)?

Welcome all.
I'm learning to develop on Assembler
During the training, a task arose - writing a calculator.
I'm writing under Linux(Ubuntu), so I have to learn the GAS(AT&T) syntax.
There was a problem with the output, namely when compiling this code:

.data
number:
  .long 0x00000595
str:
  .space 4, 0
i:
  .byte 0

str_len =.-str

.text
  .global _start
_start:

  0:
    addb $str, i
    movb $0x30, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  1:
    addb $str, i
    movb $0x31, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  2:
    addb $str, i
    movb $0x32, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  3:
    addb $str, i
    movb $0x33, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  4:
    addb $str, i
    movb $0x34, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  5:
    addb $str, i
    movb $0x35, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  6:
    addb $str, i
    movb $0x36, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  7:
    addb $str, i
    movb $0x37, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  8:
    addb $str, i
    movb $0x38, (%eax)
    incb %al
    movb %al, (i)
    jmp logic

  9:
    addb $str, i
    movb $0x39, (%eax)
    incb %al
    movb %al, (i)
    jmp logic


  logic:
    movl $number, %eax
    cdq
    movl $10, %ebx
    idivl %ebx

    cmpl $0, %edx
    je 0
    cmpl $1, %edx
    je 1
    cmpl $2, %edx
    je 2
    cmpl $3, %edx
    je 3
    cmpl $4, %edx
    je 4
    cmpl $5, %edx
    je 5
    cmpl $6, %edx
    je 6
    cmpl $7, %edx
    je 7
    cmpl $8, %edx
    je 8
    cmpl $9, %edx
    je 9

    movl $4, %eax
    movl $1, %ebx
    movl $str, %ecx
    movl $str_len, %edx
    int $0x80

    movl $1, %eax
    xor %ebx, %ebx
    int $0x80

When compiling, the following error appears:
$ as --64 test_print.s -o test_print.o
$ ld -melf_x86_64 -s test_print.o -o test_print
test_print.o: In function `_start':
(.text+0x7): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x21): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x3b): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x55): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x6f): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x86): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0x9d): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0xb4): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0xcb): relocation truncated to fit: R_X86_64_8 against `.data'
(.text+0xe2): relocation truncated to fit: R_X86_64_8 against `.data'

Help me understand, I broke my whole brain.
PS I would be very grateful if you tell me, based on the output script, a way to get a number from a character array when you enter

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-01-29
@Chame1eon

I write under Linux (Ubuntu), therefore I have to master the syntax of GAS (AT & T)

No, not "therefore". gas can handle both intel syntax (.intel_syntax) and at&t syntax (.att_syntax).
addb $str, i
relocation truncated to fit: R_X86_64_8 against `.data'

Because when building for 64 bits, you need to address relative to% rip. If you don't need it, it's easier to compile for 32 bits.
int $0x80

Does not work in 64-bit programs. Well, you got it. (with)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question