0

compiling this simple code should return 2000 but it returns 208 because it gets truncated to i8.

define i32 @"main"()
{
entry:
  %"x" = alloca i32
  store i32 2000, i32* %"x"
  %".3" = load i32, i32* %"x"
  ret i32 %".3"
}

this is how I compiled it:

llc -filetype=obj output.ll -o output.o
gcc output.o -o output -ggdb

the debugger shows the correct value to store 0x7d0, 2000 but the end result returns 208

->  0x401110 <+0>:  movl   $0x7d0, -0x4(%rsp)        ; imm = 0x7D0 
    0x401118 <+8>:  movl   $0x7d0, %eax              ; imm = 0x7D0 
    0x40111d <+13>: retq   
    0x40111e:       addb   %al, (%rax)
Process 4869 exited with status = 208 (0x000000d0) 

is this because I am using the terminal? or should I use stdout using llvm c printf?

Thank you in advance

compiling this simple code should return 2000 but it returns 208 because it gets truncated to i8.

TAW
  • 13
  • 4

1 Answers1

1

this fixed it, the terminal truncates the result to i8, I just used printf:

@str_fmt = private unnamed_addr constant [3 x i8] c"%d\00"

declare i32 @printf(i8*, ...)

define i32 @"main"()
{
entry:
  %"x" = alloca i32
  store i32 2000, i32* %"x"
  %".3" = load i32, i32* %"x"
  call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str_fmt, i32 0, i32 0), i32 %".3")
  ret i32 %".3"
}
TAW
  • 13
  • 4
  • In Unix exit codes are in the range 0-255. Your program correctly returns 2000, but the shell truncates the returned exit code. Your approach of printing the number using `printf` is correct. – Martin Fink Mar 14 '23 at 15:15