Recent Posts

Pages: [1] 2 3 ... 10
1
Using NASM / Re: Convert MASM code to NASM
« Last post by debs3759 on October 29, 2020, 03:36:04 PM »
I have written a similar version to your last post, with some small changes. Personally I would create callable functions for repeated code, but I wanted to keep it as similar to your code as possible, to help you understand the changes.

I kept your labels so you can study each block separately. I always fully comment my code, to make it easier to understand.

This code can be assembled into a DOS .com file. I didn't test it, but believe it is OK. To convert to Linux code, you will need to look up Linux system calls and/or get more help elsewhere.

Code: [Select]
section .data
        msg1 db 'Please enter the first 8-bit binary number: ', 0DH, 0AH
        msg2 db 'Please enter the second 8-bit binary number: ', 0DH, 0AH
        msg3 db  0DH, 0AH, 'The sum of the given binary numbers is:', 0DH, 0AH
        invalid   db  'Invalid input, try again.', 0DH,0AH


section .text

org 100h                                 ; only needed if assembling as a DOS .com file
_start:
        push cs
        pop ds                           ; set data segment

        xor bx, bx                       ; used to store input

        lea dx, msg1                     ;
        mov ah, 9                        ;
        int 21h                          ; output first message using DOS interrupt

        mov cx,8                         ; loop counter
        mov ah,1                         ; DOS int 21h function for keyboard input

_loop_1:
        int 21h                          ; get character input

        sub al,"0"                       ; convert character input to binary digit
        cmp al,1                         ; is input a valid binary digit?
        ja error                         ; if not, jump to error

        shl bl,bl                        ;
        add bl,al                        ; store this character

        loop _loop_1                     ; fetch next digit

        int 21h                          ; fetch final character
        cmp al,0Dh                       ; is this a carriage return?
        ja error                         ; if not, jump to error

        lea dx, msg2                     ;
        mov ah, 9                        ;
        int 21h                          ; output second message using DOS interrupt

        mov cx,8                         ; loop counter
        mov ah,1                         ; DOS int 21h function for keyboard input

_loop_2:
        int 21h                          ; get character input

        sub al,"0"                       ; convert character input to binary digit
        cmp al,1                         ; is input a valid binary digit?
        ja error                         ; if not, jump to error

        shl bh,bh                        ;
        add bh,al                        ; store this character

        loop _loop_2                     ; fetch next digit

        int 21h                          ; fetch final character
        cmp al,0Dh                       ; is this a carriage return?
        ja error                         ; if not, jump to error

        lea dx, msg3                     ;
        mov ah, 9                        ;
        int 21h                          ; output third message using DOS interrupt

        mov ah,2                         ; DOS function 2 outputs a single character
        adc bl,bh                        ; add both numbers, using carry flag for overflow
        jnc _next                        ; if result is only 8-bits, skip this short section

        mov dl,"1"
        int 21h                          ; output "1"

_next:       
        mov cx,8
        xor bh,bh

_loop_3:
        shl bx,1
and bh,1                         ; is next digit 0 or 1?
        jnz _one

        mov dl,"0"                       ; next digit is 0
        jmp _display:

_one:
        mov dl,"1"                       ; next digit is 1

_display
        int 21h                          ; output this character
        loop _loop_3

_exit:
        mov ax,04C00h
        int 21h                          ; exit with error code 0

_error:
        lea dx, invalid                  ;
        mov ah, 9                        ;
        int 21h                          ; output error message using DOS interrupt

        jmp _start:
2
Programming with NASM / Re: How do I add binary number in NASM?
« Last post by Azrael998 on October 29, 2020, 06:08:27 AM »
Thank you very much Frank for all your help.
3
Programming with NASM / Re: How do I add binary number in NASM?
« Last post by Frank Kotler 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

4
Programming with NASM / Re: How do I add binary number in NASM?
« Last post by Frank Kotler on October 29, 2020, 03:58:59 AM »
Yeah, that doesn't work. Maybe later. Sorry.

Best,
Frank

5
Programming with NASM / Re: How do I add binary number in NASM?
« Last post by Azrael998 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.
6
Programming with NASM / Re: How do I add binary number in NASM?
« Last post by Frank Kotler 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

7
Programming with NASM / How do I add binary number in NASM?
« Last post by Azrael998 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
8
Using NASM / Re: Convert MASM code to NASM
« Last post by debs3759 on October 28, 2020, 01:22:53 PM »
As the numbers are being input in ascii text (you are inputting the numbers as ascii codes, not as numbers) I do think it is more efficient, yes. For example, when you type "0", you are not storing 0, you are storing the ascii code, which is 30h (48 decimal). Also, by doing the conversion, you can just add the numbers in both registers (which will set the overflow flag if the result is higher than 0FFh). If you don't convert, you will still have to subtract 30h from each element, then you will have to check each element of the result to see if it is higher than "1". That just seems inefficient to me.

You will also find that, if you write efficient callable routines, they will be reusable in other apps, and will form a template you only need minor adjustments to for similar routines.
9
Using NASM / Re: Convert MASM code to NASM
« Last post by Azrael998 on October 28, 2020, 11:54:20 AM »
I'm planning on rewriting the code do you think it's efficient to convert the binary numbers to decimal then add them then convert the result to binary?
10
Using NASM / Re: Convert MASM code to NASM
« Last post by debs3759 on October 28, 2020, 07:26:31 AM »
Here is some basic pseudo code. I can expand on some of it using code that works on DOS or uses BIOS interrupts, but that would still need changing to use Linus system calls.

section .data
        msg1 db 'Enter the first binary number: ', 0DH, 0AH
        msg2 db 'Enter the second binary number: ', 0DH, 0AH
        msg3 db 'The sum of the given binary numbers: ', 0DH, 0AH
        illegal   db  'Illegal character. Try again.', 0DH,0AH
        str1  times 10 db 0
        str2  times 10 db 0
        result  times 11 db 0



section .text

        org     256           ; only needed if you code for a DOS environment and create a .com file

        read input strings from stdin (normally keyboard)(I would create a callable routine
                                                which takes the output address as its input)
        convert ascii string to binary digits, checking each character as you go.
                                                Store in bh and bl if the systems calls preserve them
        if any input character is invalid, output error msg
        if no errirs, add the resulting numbers
        convert back to am ascii string
        display the output



I made the strings longer than you need so that you can test for input that is too long, as well as having room for the carriage returm (to help check the length of the input). DOS has an interrupt that takes the length of input as an argument, so I assume Linux has that as well,
Pages: [1] 2 3 ... 10