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

Offline Azrael998

  • Jr. Member
  • *
  • Posts: 12
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
   
   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
« Last Edit: October 29, 2020, 02:54:31 AM by Azrael998 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
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 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

Warning! Untested code. I should know better!

Best,
Frank


Offline Azrael998

  • Jr. Member
  • *
  • Posts: 12
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.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
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


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
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, 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

I think that gets it. Only shift bl!

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

Best,
Frank


Offline Azrael998

  • Jr. Member
  • *
  • Posts: 12
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.