d1w3
d1w3

Reputation: 13

Assistance on x86 Assembler running on Linux

I am currently learning a bit of Assembler on Linux and I need your advice. Here is the small program:

.section .data
  zahlen:
   .float 12,44,5,143,223,55,0

.section .text
.global _start

_start:
    movl $0, %edi
    movl zahlen (,%edi,4), %eax
    movl %eax, %ebx

 begin_loop:
   cmpl $0, %eax
   je prog_end
   incl %edi
   movl zahlen (,%edi,4), %eax
   cmpl %ebx, %eax
   jle begin_loop
   movl %eax, %ebx
   jmp begin_loop

prog_end:
   movl $1, %eax
   int $0x80

The program seems to compiling and running fine. But I have some unclear questions/behaviors:

  1. if I check the return value, which is the highers number in register %ebx, with the command "echo %?" it always return 0. I expect the value 223. Any Idea why this happens?

  2. I checked with DDD and gdb compiling with debugging option. So i saw that the program runs the correct steps. But if i want to exam the register with command ie. "i r eax" it only shows me the address i believe, not the value. Same on DDD. I see only registers rax rbx and so on. Here i need some advise to get on the right track. Any Help appreciated. Thanks

Upvotes: 1

Views: 70

Answers (2)

puppydrum64
puppydrum64

Reputation: 1688

The "main" registers eax, ebx, ecx, edx, etc. are all designed to work with integers only. A float is a shorthand term that typically refers to a very specific data format (namely, the IEEE-754 binary32 standard), for which your CPU has dedicated registers and hardware to work with. As you saw, you are allowed to load them into integer registers as-is, but the value isn't going to convert itself like it would in a high-level, dynamically-typed language. Your code loaded the raw bit pattern instead, which likely is not at all what you intended.

This is because assembly has no type safety or runtime type-checking. The CPU has no knowledge of what type you declared your data as in your program. So when loading from memory into eax the CPU assumes that the data is a 32-bit integer, even if you declared it in your source code as something else.

If you're curious as to what a float actually looks like you can check this out: Floating Point to Hex Calculator

Upvotes: 1

d1w3
d1w3

Reputation: 13

Switching from float to long solved the problem. Think mistake by myself. Also compiling and linking as 32bit shows the right registers in the debugger.

Upvotes: 0

Related Questions