I found a strange issue when using multiplication (*=) and one of the operands is 1.
C code: `int printf(const char* str, ...);
int main() { int x = 1; x *= 100; printf("%d\r\n", x); }`
asm code:
section .data section .text extern _printf global main ; main function main: push ebp mov ebp, esp sub esp, 16 push dword 1 pop eax mov dword [ebp-4], eax push dword 100 pop eax mov ecx, eax mov eax, [ebp-4] imul eax mov dword [ebp-4], eax lea ebx, [_printf] push ebx pop ebx mov dword [function_call_1], ebx push dword [ebp-4] mov eax, str_2 push eax call [function_call_1] add esp, 8 push eax pop eax push eax add esp, 4 add esp, 16 pop ebp ret section .data function_call_1: dd 0 section .rodata str_2: db '%', 'd', 0
The function printf always outputs 1, but I change int x = 1 to int x = 2, it outputs correctly. Can you tell me why? the multiplication operand can not be 1?
*= is handled are you basing the issue from the master repository or an earlier commit?
Thanks
Can you format your assembly so its not on one line please
section .data section .text extern printf global main ; main function main: push ebp mov ebp, esp sub esp, 16 push dword 1 pop eax mov dword [ebp-4], eax push dword 100 pop eax mov ecx, eax mov eax, [ebp-4] imul eax mov dword [ebp-4], eax lea ebx, [printf] push ebx pop ebx mov ecx, ebx push dword [ebp-4] mov eax, str_1 push eax call ecx add esp, 8 push eax pop eax push eax add esp, 4 add esp, 16 pop ebp ret section .rodata str_1: db '%', 'd', 13, 10, 0
Above asm code was formated. I wrote code follow your lecture video, even I got the C code from https://github.com/nibblebits/PeachCompiler.git, but the problem still exists.
George Chow
Okay, please can you clone the repository from the master and if the same problem persists its a bug. If it doesnt persist then you made a mistake, please let me know
Thanks
Here are detailed steps I did again today:
step 1, git clone https://github.com/nibblebits/PeachCompiler.git
step 2, replace content of test.c with: int printf(const char* str, ...);
int main() { int x = 1; x *= 100; printf("%d\n", x); }
step 3, make clean && make
step4: george@george-ubuntu:~/Desktop/gitcompiler/PeachCompiler$ ./main section .data section .text extern printf global main ; main function main: push ebp mov ebp, esp sub esp, 16 push dword 1 pop eax mov dword [ebp-4], eax push dword 100 pop eax mov ecx, eax mov eax, [ebp-4] imul eax mov dword [ebp-4], eax lea ebx, [printf] push ebx pop ebx mov dword [function_call_1], ebx push dword [ebp-4] mov eax, str_2 push eax call [function_call_1] add esp, 8 push eax pop eax push eax add esp, 4 add esp, 16 pop ebp ret section .data function_call_1: dd 0 section .rodata str_2: db '%', 'd', 10, 0 everything compiled file /usr/bin/ld: ./test.o: warning: relocation in read-only section `.text' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE nasm -f elf32 ./test -o ./test.o && gcc -m32 ./test.o -o ./test
george@george-ubuntu:~/Desktop/gitcompiler/PeachCompiler$ ./test 1
And this is my operate system info: george@george-ubuntu:~/Desktop/gitcompiler/PeachCompiler$ uname -a Linux george-ubuntu 5.15.0-57-generic #63-Ubuntu SMP Thu Nov 24 13:43:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Hi, Yes its a valid bug thank you for reporting it after some debugging ive found the problem: See the debugging window: https://ibb.co/3mTxd0R
The two operands are calculated correctly, the issue lies in that the IMUL instruction should of mulitplied on the ECX register but multiplies on the EAX register which leads to 1 multiplied by 1 causing this problem.
Thanks for reporting this I will look into fixing it.
Hello, Thanks for reaching out, once theres enough issue reports I will go through them all and create a lecture addressing any issues.
Thanks Dan