0

I'm very new to Assembly and I've literally spent days staring at my computer screen trying to figure this out. The program is supposed to add the binary numbers 1101 and 0111, then print their sum in binary form. Can someone please help me figure this out?.

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
  • You're outputting raw binary to stdout, which is fine if you pipe it into a hexdump program that has an option to dump in base 2 instead. `./a.out | xxd -b` [Can hexdump (hd) or octal dump (od) be used to display binary data as zeros and ones?](https://superuser.com/q/897685) – Peter Cordes Oct 29 '20 at 03:14
  • Or [Converting decimal to binary in assembly x86](https://stackoverflow.com/q/22988985) shows one algorithm. [How to create a byte out of 8 bool values (and vice versa)?](https://stackoverflow.com/a/51750902) shows a more efficient clever trick using a multiply with a magic constant to unpack 8 bits at once into 8 bytes. – Peter Cordes Oct 29 '20 at 03:17
  • Assuming you want to `write` an ASCII string with `'0'` and `'1'` digits, [Creating an x86 assembler program that converts an integer to a 16-bit binary string of 0's and 1's](https://stackoverflow.com/q/40811218) shows how to do that. [How to create a byte out of 8 bool values (and vice versa)?](https://stackoverflow.com/a/51750902) would be a more efficient way to convert only 8 bits, using a full `mul` to produce a result in EDX:EAX for `add eax, '0000'` / `add edx, '0000'`. Then I guess `bswap` each reg to get it in most-significant-first (lowest address) printing order. – Peter Cordes Oct 29 '20 at 03:44

0 Answers0