### Author Topic: How do I add binary number in NASM?  (Read 37 times)

#### Azrael998

• Jr. Member
• Posts: 9
##### How do I add binary number in NASM?
« on: October 29, 2020, 01:37:33 AM »
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

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
« Last Edit: October 29, 2020, 02:54:31 AM by Azrael998 »

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2451
• Country:
##### Re: How do I add binary number in NASM?
« Reply #1 on: October 29, 2020, 03:14:01 AM »

Code: [Select]
`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 stringmov ebx, [sum] ; get our numberxor edx, edx ; make edx 0mov  edi, buffer ; someplace to put it.topmov al, '0'shl ebx, 1adc  al, 0stosbinc edxcmp edx, 8jnz .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 .datamsg db "The sum is: "len equ \$ - msg   segment .bsssum resb 16   ; only need 4buffer resb 8`
Warning! Untested code. I should know better!

Best,
Frank

#### Azrael998

• Jr. Member
• Posts: 9
##### Re: How do I add binary number in NASM?
« Reply #2 on: October 29, 2020, 03:29:11 AM »
Thank you very much Frank for responding. I tested the code on my terminal but the result that gets printed is 00000000.

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2451
• Country:
##### Re: How do I add binary number in NASM?
« Reply #3 on: October 29, 2020, 03:58:59 AM »
Yeah, that doesn't work. Maybe later. Sorry.

Best,
Frank

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2451
• Country:
##### Re: How do I add binary number in NASM?
« Reply #4 on: October 29, 2020, 05:30:51 AM »
Code: [Select]
`section   .text   global _start        ;must be declared for using gcc   _start:                   ;tell linker entry point   mov   eax, 1101b      mov    ebx, 0111b      add    eax, ebxno!;   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 stringmov ebx, [sum] ; get our numberxor edx, edx ; make edx 0mov  edi, buffer ; someplace to put it.topmov al, '0'shl bl, 1 ; only bl !!!adc  al, 0stosbinc edxcmp edx, 8jnz .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 .datamsg db "The sum is: "len equ \$ - msg   segment .bsssum resb 16   ; only need 4buffer resb 8`
I think that gets it. Only shift bl!

For a minute there I thought I was getting "too" old!

Best,
Frank

#### Azrael998

• Jr. Member
• Posts: 9
##### Re: How do I add binary number in NASM?
« Reply #5 on: October 29, 2020, 06:08:27 AM »
Thank you very much Frank for all your help.