Posted a question that terminates the program on the assembly
I am writing a program to calculate the bmi (Body Mass Index) in the assembly for practice, but I put out a segmentation fault in the middle and it stops.
Why is that?
OS:ubuntu 16.04 x86_64
Compile as 32-bit with gcc-m32
.data
message:.string "file open error\n"
your_height:.string "Please enter the your height(m):"
your_weight:.string "Please enter the your weight(kg):"
newline: .string "\n"
.bss
tmp: .skip128,0x00
.text
.global main
main:
push%ebp
movl%esp, %ebp
pushl$your_height
call print
call read
pushl$your_weight
call print
call read
movl%esp,%eax
movl20(%esp), %ebx
divl%ebx
divl%ebx
pushl%eax
call print
call exit
read:
movl$3,%eax
movl$0,%ebx
movl$tmp, %ecx
movl$64,%edx
int$0x80
cmpl$-1,%eax
je write_error_message
movl$tmp, %esi
pushl%esi
ret
write_error_message:
movl $4,%eax
movl$1,%ebx
movl$emessage, %ecx
movl $16,%edx
int$0x80
call exit
exit:
movl$1,%eax;
xorl%ebx,%ebx;
int$0x80
print:
movl $4,%eax
US>movl4(%esp), %ecx
movl$1,%ebx
movl$1,%edx
write:
cmpb$0x00, (%ecx)
jeret_func
movl $4,%eax
int$0x80
include %ecx
jmp write
ret_func:
ret
assembly-language
The direct cause is ret
while read
is destroying stack frames.
I'd like to say that it will be fixed if you correct it, but that won't be what I expected.
A system call read()
takes a byte string from a file descriptor.
123<line feed>
results from 31
32
33
0A
.
In addition, atoi
and atof
are required to enter the height and weight numerical values.
pushl%esi
is probably part of the function call instruction string that causes the presentation code to drop
It is estimated that the program itself is dead there because it is incomplete.
c compiler or C Runtime Library
is in front of you and ready to use.
The Euler does not want to write the equivalent of atoi
strtol directly in the assembler.
(I have written the equivalent function in c.It's only an integer operation, so it's not very difficult.)
atof
strtod
I don't want to write the equivalent function even if it's c.What's more, the assembler.
(There are too many things to consider, such as poor accuracy and rounding.)
Why don't you learn how to call the c library function?
It also gives you the knowledge to create an assembler function called the
In modern times, I think it is very rare to make software that can only be completed with an assembler.
The function call convention of x86 is troublesome because there are many cdecl and stdcall.
First, you should read the assembler that the c compiler writes.
No optimization is recommended, as optimization (strong) will prevent even skilled people from reading it.
gcc-S-O0example.c
© 2024 OneMinuteCode. All rights reserved.