NASM Forum > Using NASM
Convert MASM code to NASM
Azrael998:
Hello, everyone, I'm very new to assembly and I'm trying to convert this MASM code to NASM for an assignment, can anyone please help me?. The code is supposed to ask a user for two binary numbers add them then print their sum in binary form.
MASM code is below:
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 0DH,0AH,\'Enter the first binary number ( max 8-digits ) : $\'
PROMPT_2 DB 0DH,0AH,\'Enter the second binary number ( max 8-digits ) : $\'
PROMPT_3 DB 0DH,0AH,\'The SUM of given binary numbers in binary form is : $\'
ILLEGAL DB 0DH,0AH,\'Illegal character. Try again.$\'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
JMP @START_2 ; jump to label @START_2
@START_1: ; jump label
LEA DX, ILLEGAL ; load and display the string ILLEGAL
MOV AH, 9
INT 21H
@START_2: ; jump label
XOR BX, BX ; clear BX
LEA DX, PROMPT_1 ; load and display the string PROMPT_1
MOV AH, 9
INT 21H
MOV CX, 8 ; initialize loop counter
MOV AH, 1 ; set input function
@LOOP_1: ; loop label
INT 21H ; read a character
CMP AL, 0DH ; compare AL with CR
JNE @SKIP_1 ; jump to label @SKIP_1 if AL!=0DH
CMP CX, 8 ; compare CX with 8
JE @START_1 ; jump to label @START_1 if CX=8
JMP @EXIT_LOOP_1 ; jump to label @EXIT_LOOP_1
@SKIP_1: ; jump label
AND AL, 0FH ; convert ascii into decimal code
SHL BL, 1 ; shift BL towards left by 1 position
OR BL, AL ; set the LSB of BL with LASB of AL
LOOP @LOOP_1 ; jump to label @LOOP_1 if CX!=0
@EXIT_LOOP_1: ; jump label
LEA DX, PROMPT_2 ; load and display the string PROMPT_2
MOV AH, 9
INT 21H
MOV CX, 8 ; initialize loop counter
MOV AH, 1 ; set input function
@LOOP_2: ; loop label
INT 21H ; read a character
CMP AL, 0DH ; compare AL with CR
JNE @SKIP_2 ; jump to label @SKIP_2 if AL!=0DH
CMP CX, 8 ; compare CX with 8
JE @START_2 ; jump to label @START_2 if CX=8
JMP @EXIT_LOOP_2 ; jump to label @EXIT_LOOP_2
@SKIP_2: ; jump label
AND AL, 0FH ; convert ascii into decimal code
SHL BH, 1 ; shift BH towards left by 1 position
OR BH, AL ; set the LSB of BH with LASB of AL
LOOP @LOOP_2 ; jump to label @LOOP_2 if CX!=0
@EXIT_LOOP_2: ; jump label
LEA DX, PROMPT_3 ; load and display the string PROMPT_3
MOV AH, 9
INT 21H
ADD BL, BH ; add BL and BH
JNC @SKIP ; jump to label @SKIP if CF=1
MOV AH, 2 ; print the digit 1 i.e. carry
MOV DL, 31H
INT 21H
@SKIP: ; jump label
MOV CX, 8 ; initialize loop counter
MOV AH, 2 ; set output function
@LOOP_3: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
JC @ONE ; jump to label @ONE if CF=1
MOV DL, 30H ; set DL=0
JMP @DISPLAY ; jump to label @DISPLAY
@ONE: ; jump label
MOV DL, 31H ; set DL=1
@DISPLAY: ; jump label
INT 21H ; print the character
LOOP @LOOP_3 ; jump to label @LOOP_3 if CX!=0
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN
debs3759:
Have you assembled this with MASM and tested it? It looks to me like it will never display the error message. I could translate it to nasm code, but you need to get it working first, or at least try fixing it and ask about specific parts that don't work. I can't do all your homework without knowing you have tried :)
If I were to write code to do what you want, I would create macros and/or callable routines.
Azrael998:
I don't know if this is right but this is what I currently have from reading the Assembly tutorial from tutorialspoint.com. I get the error Segmentation fault (core dumped) when I run the code in my terminal. Can you tell me what the '@' sign does in MASM. I'm only slightly familiar with NASM and I've never used MASM before.
NASM code:
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
section .text
global _start
_start:
_start_1:
lea dx, illegal
mov ah, 9
int 21h
_start_2:
xor bx, bx
lea dx, msg1
mov ah, 9
int 21h
mov cx, 8
mov ah, 1
_loop_1:
int 21H
cmp al, 0DH
jne _skip_1
cmp cx, 8
je _start_1
jmp _exit_loop_1
_skip_1:
and al, 0FH
shl bl, 1
loop _loop_1
_exit_loop_1:
lea dx, msg1
mov ah, 9
int 21H
mov cx, 8
mov ah, 1
_loop_2:
int 21H
cmp al, 0DH
jne _skip_2
cmp cx, 8
je _start_2
jmp _exit_loop_2
_skip_2:
and al, 0FH
shl bh, 1
or bh, al
loop _loop_2
_exit_loop_2:
lea dx, msg3
mov ah, 9
int 21H
add bl, bh
jnc _skip
mov ah, 1
mov dl, 31H
int 21H
_skip:
mov cx, 8
mov ah, 2
_loop_3:
shl bl, 1
jc _one
mov dl, 30H
jmp _display
_one:
mov dl, 31H
_display:
int 21H
loop _loop_3
mov ah, 4ch
int 21H
ret
debs3759:
The @ tells masm that it is a label. Your code looks like it is written to be a DOS .com file. I don't know what OS your terminal is running - is it DOS? If not, that might be your problem. You don't need the _ in front of labels unless you are creating object files to link with in C (although it doesn't hurt to have them, they just aren't needed).
Also, the rewritten code will now write the error message then carry on to execute the rest of your code.
When coding, I always start by writing pseudo code (basically mapping out what routines need to happen in what order, and what I need to happen. I find that the easiest and fastest way to get everything working.
I'll take a better look after I have slept, and offer you some better suggestions, then when you have done what you can with them, I'll try to help improve your code. Is this for fun or for school?
Azrael998:
Thank you very for responding this is for school. My terminal is running on manjaro linux x86_64, the MASM code was written by my professor but I don't know what OS his terminal is running on.
Navigation
[0] Message Index
[#] Next page
Go to full version