5

Good evening people. I just started learning about assembly language, and I've found very good examples online to follow. Someone recommended me to use NASM to start learning, but the examples I've found I've seen they use 8086 assembler. As I was following the examples, I noticed that if I used NASM they didn't run on a linux terminal, however if I installed an 8086 assembler emulator, it did work.

My question is: is there any difference between NASM and 8086 assembler? to me they should be the same thing since it is assembly code. If not, is there anyone that could explain me if there is ANY difference between both? and why they don't run being the same code on both NASM and 8086

this is the code I was following that did run on 8086 assembler but not on NASM on Linux

 DATA SEGMENT
     NUM1 DB ?
     NUM2 DB ?
     RESULT DB ?
     MSG1 DB 10,13,"ENTER FIRST NUMBER TO ADD : $"
     MSG2 DB 10,13,"ENTER SECOND NUMBER TO ADD : $"  
     MSG3 DB 10,13,"RESULT OF ADDITION IS : $"
ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX

      LEA DX,MSG1
      MOV AH,9
      INT 21H

      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM1,AL

      LEA DX,MSG2
      MOV AH,9
      INT 21H

      MOV AH,1
      INT 21H
      SUB AL,30H
      MOV NUM2,AL

      ADD AL,NUM1

      MOV RESULT,AL

      MOV AH,0 
      AAA

      ADD AH,30H
      ADD AL,30H 

      MOV BX,AX

      LEA DX,MSG3
      MOV AH,9
      INT 21H

      MOV AH,2
      MOV DL,BH
      INT 21H

      MOV AH,2
      MOV DL,BL
      INT 21H

      MOV AH,4CH
      INT 21H     
ENDS
END START

Thanks and hope you have a good night.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
mr_nyo
  • 99
  • 1
  • 9
  • 4
    Emu8086 is based on MASM/TASM syntax. NASM uses a different syntax than MASM?TASM. Each assembler has its own syntax, but they still produce x86 instructions. You will need to convert your Emu8086 code to NASM syntax for it to assemble properly. – Michael Petch Oct 20 '17 at 05:37
  • 4
    Your code is for 16 bit using DOS system calls. It won't run natively under Linux, but you could use DOSBOX or BOCHS to run it. (Different OSes use different system calls, and different calling conventions for them. See https://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-on-i386-and-x86-6. – Peter Cordes Oct 20 '17 at 05:46
  • 1
    And the x86 CPUs have several different runtime modes, after power on they start in 16b "real mode", do the BIOS start code and with legacy BIOS (not UEFI or how is the new thing called) it will load bootsector from some storage device and execute it. Then usually any modern bootloader will switch CPU to 32b "protected mode" and load OS, modern 64b OS will then switch to 64b at start and operate under it. The old DOS did stay in 16b "real mode" and provides services under it. So when you write 16b asm in NASM, use [bits 16], etc... each mode requires slightly different opcodes. – Ped7g Oct 20 '17 at 09:45
  • 1
    If you are just starting to learn x86 assembly, I would probably focus on mix of C code + x86 32b asm, as that one is most straightforward and with minimal constraints. I.e. take linux gcc, search how to produce 32 bit binaries, and how to link 32b nasm object files against it, then you can call from assembly libc functions like `printf` and similar (the linux services are very limited, and even just simple things like printing decimal number require writing your own string formatter, so having libc functions for a start is help, especially if you already know C). After 32b learn 64b + 16b. – Ped7g Oct 20 '17 at 09:48
  • Then again doing the 16b thing under dosbox may be a bit more fun, as you can use the old direct video ram modifications to do some graphics fun with old VGA modes, which are simple to grasp and don't require specialized gfx driver. To do anything "pretty" directly under linux your best bet is probably to link against SDL-like library, or use OpenGL to do 3D graphics, neither of that is beginner-like stuff, so learning 32b asm will make you stay longer in the land of console input/output (or produce simple bitmap into memory and draw it in C with SDL/OGL/etc to avoid those APIs in asm). – Ped7g Oct 20 '17 at 09:51
  • Thanks for all of your responses guys!!! now I can understand better why the differences. and I'll definitely will try to convert/mix with c/learn the 32 to 64b. – mr_nyo Oct 20 '17 at 13:44

0 Answers0