Discussion:
Nasm error undefined reference to printf
Add Reply
Mohammad Fayad
2021-12-20 01:04:09 UTC
Reply
Permalink
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
Frank Kotler
2021-12-20 05:20:55 UTC
Reply
Permalink
Post by Mohammad Fayad
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,
Post by Mohammad Fayad
global _start
extern _printf
No underscore in GNU C?

Best.
Frank
wolfgang kern
2021-12-20 09:29:11 UTC
Reply
Permalink
Post by Mohammad Fayad
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
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
2021-12-20 09:55:42 UTC
Reply
Permalink
Post by Mohammad Fayad
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
  test[variable],01
  jnz not_by_2
  test[variable],07
  jnz by_2_but_not_by8
...
 test[variable],15
 ;dividable by 2 and 8 if four least significant bits are zero
 jnz failed
...
oops :):)
if a number is dividable by 8 then it's dividable by 2 as well

__
wolfgang

Loading...