Nasm error undefined reference to printf

42 views
Skip to first unread message

Mohammad Fayad

unread,
Dec 20, 2021, 12:09:48 AM12/20/21
to
Hello this is my code im trying to do a program that checks if a number is divisble by 2 and 8 and im getting an error undefined printf
This is the code:

global _start

extern _printf

SECTION .data
message1: db "Enter a number: ", 0
number1: db "%d", 0
integer1: times 10 db 0 ; 32-bits integer = 10 bytes
msg db 'Divisible by 2 and 8', 0xa,0xd
len equ $ - msg
SYS_EXIT equ 1
STDOUT EQU 1
SYS_WRITE EQU 4
SECTION .bss
val2 resb 2

SECTION .text

_start:

push message1
call _printf
pop rbx
mov eax, 3
mov ebx, 1
mov ebx, val2
mov edx, 2
int 0x80
push val2
mov al, [val2]

l1:
.divisble_by_2:
mov ax, [number1]
xor dx, dx
mov bx, 2
div bx
cmp dx, 0
jnz .not_divisible

.divisble_by_8:
mov ax, [number1]
xor dx, dx
mov bx, 8
div bx
cmp dx, 0
jnz .not_divisible

.print_number:
mov edx, [number1]
add edx, 48
mov [number1], edx

mov eax, 4
mov ebx, 1
mov ecx, [number1]
mov edx, len
int 0x80
mov eax, .divisble_by_2
int 0x80

.not_divisible:
xor eax, eax
mov edx, [eax+len]
mov al, 1
mov esi, .divisble_by_2
mov edi, eax
mov eax, 1
int 0x80
jmp _start

Mohammad Fayad

unread,
Dec 20, 2021, 12:11:23 AM12/20/21
to
Hello im trying to write a program that checks if a number is divisble by 2 and 8 and prints numbers in loop and im getting error undefined reference to printf

Frank Kotler

unread,
Dec 20, 2021, 12:27:13 AM12/20/21
to
On 12/19/2021 08:04 PM, Mohammad Fayad wrote:
> Hello this is my code im trying to do a program that checks if a number is divisble by 2 and 8 and im getting an error undefined printf

Hi Mohammad,

> This is the code:
>
> global _start
>
> extern _printf

No underscore in GNU C?

Best.
Frank

wolfgang kern

unread,
Dec 20, 2021, 4:42:28 AM12/20/21
to
On 20/12/2021 02:04, Mohammad Fayad wrote:
> Hello this is my code im trying to do a program that checks if a number is divisble by 2 and 8 and im getting an error undefined printf
> This is the code:

cant help with the error but:
dividable by two:
test[variable],01
jnz not_by_2
dividable by eight:
test[variable],07
jnz by_2_but_not_by8
by2and8:
...

or much easier:
test[variable],15
;dividable by 2 and 8 if four least significant bits are zero
jnz failed
by2and8:
...
__
wolfgang

wolfgang kern

unread,
Dec 20, 2021, 4:57:30 AM12/20/21
to
oops :):)
if a number is dividable by 8 then it's dividable by 2 as well

__
wolfgang


Reply all
Reply to author
Forward
0 new messages