NASM Forum > Programming with NASM
How do I add binary number in NASM?
Azrael998:
I have spent 8 days trying to figure out how I can add binary numbers in NASM, I've searched in stackoverflow multiple times and got nothing can somebody please help me?. This is for an assignment I have for school, the submission for the assignment is today. I'm trying to add the binary numbers 1101 and 0111 then print their result in binary form. Can someone please help me figure out where I went wrong?.
NASM code:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, 1101b
mov ebx, 0111b
add eax, ebx
mov [sum], eax
mov ecx, msg
mov edx, len
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx, sum
mov edx, 1
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The sum is: "
len equ $ - msg
segment .bss
sum resb 16
Frank Kotler:
--- Code: ---section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, 1101b
mov ebx, 0111b
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx, msg
mov edx, len
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
; convert binary to string
mov ebx, [sum] ; get our number
xor edx, edx ; make edx 0
mov edi, buffer ; someplace to put it
.top
mov al, '0'
shl ebx, 1
adc al, 0
stosb
inc edx
cmp edx, 8
jnz .top
mov ecx, buffer
; edx is 8
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The sum is: "
len equ $ - msg
segment .bss
sum resb 16 ; only need 4
buffer resb 8
--- End code ---
Warning! Untested code. I should know better!
Best,
Frank
Azrael998:
Thank you very much Frank for responding. I tested the code on my terminal but the result that gets printed is 00000000.
Frank Kotler:
Yeah, that doesn't work. Maybe later. Sorry.
Best,
Frank
Frank Kotler:
--- Code: ---section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, 1101b
mov ebx, 0111b
add eax, ebx
no!
; add eax, '0'
mov [sum], eax
mov ecx, msg
mov edx, len
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
; convert binary to string
mov ebx, [sum] ; get our number
xor edx, edx ; make edx 0
mov edi, buffer ; someplace to put it
.top
mov al, '0'
shl bl, 1 ; only bl !!!
adc al, 0
stosb
inc edx
cmp edx, 8
jnz .top
mov ecx, buffer
; edx is 8
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The sum is: "
len equ $ - msg
segment .bss
sum resb 16 ; only need 4
buffer resb 8
--- End code ---
I think that gets it. Only shift bl!
For a minute there I thought I was getting "too" old!
Best,
Frank
Navigation
[0] Message Index
[#] Next page
Go to full version