NASM Forum > Using NASM
Convert MASM code to NASM
Azrael998:
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?
debs3759:
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.
debs3759:
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: ---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:
--- End code ---
Navigation
[0] Message Index
[*] Previous page
Go to full version