0

I am learning NASM and trying to understand the codes.

I have a code like:

section .data

text:  db  "Just some text"  ;LENGTH = 14.
color: db 181          
x:     db 0
y:     db 0

section .text
  global _start

_start:

  mov  bp, text ;STRING TO DISPLAY.
  mov  cx, 14          ;STRING LENGTH.
  mov  [x], byte 50    
  mov  [y], byte 20
  call color_string

  mov  ax,4c00h
  int  21h



color_string:

  mov  ax, ds
  mov  es, ax         
  mov  ah, 13h        
  mov  bh, 0          
  mov  bl, color
  mov  dl, x         
  mov  dh, y        
  int  10h           

  ret

I am just trying to learn NASM and trying to compile this code.

Here I just want to print the text in different color. But compiling it giving me error.

If anyone also can guide me how to print the text color in loop that would be greeat like print the text 5 times with loop.

I am getting the error like:

color.o: In function `_start':
color.asm:(.text+0x2): relocation truncated to fit: R_386_16 against `.data'
color.o: In function `color_string':
color.asm:(.text+0x2b): relocation truncated to fit: R_386_8 against `.data'
color.asm:(.text+0x2d): relocation truncated to fit: R_386_8 against `.data'
color.asm:(.text+0x2f): relocation truncated to fit: R_386_8 against `.data'

I dont know what is the issue. Help needed.

Green Eagle
  • 227
  • 3
  • 12
  • 2
    where did you get `mov bl, color` line from? Is it yours or copied? It would compile in MASM with expected functionality, but in NASM you must mark every memory access with squared brackets, i.e. `mov bl, [color]` will load value 181 into register `bl`. The `mov bl, color` in NASM means "load memory address `color` into `bl`", which naturally does not fit into 8 bit register, as it looks like you are producing 32 bit executable, where addresses are 32 bit long. Also that code calls 16 bit BIOS service, which is probably not available on the platform you are working now. – Ped7g Apr 02 '18 at 12:28
  • You must use syntax of your assembler, and system services of your target platform, just copying MASM 16 bit DOS source over (for example) 32 bit linux with NASM will not work, even if you fix obvious syntax problems like missing square brackets and similar, the resulting machine code would still not work because the system service calls will be incompatible. Try to find tutorial directly targetting your platform (OS + NASM), so you don't need to learn about all the differences between different x86 modes and assemblers from the beginning, and you can focus on asm basics first. – Ped7g Apr 02 '18 at 12:30
  • @Ped7g I copied the code as I am learning.. I am trying to understand the code.. If you can then please elaborate the code .. That would be so grateful.. I am using NASM on ubuntu – Green Eagle Apr 02 '18 at 12:31
  • @Ped7g I am unware about the complete NASM syntax .. I am just learning... If you could help me with this I could learn further with syntaxes.. – Green Eagle Apr 02 '18 at 12:32
  • It's not directly convertible for NASM + Ubuntu. Ubuntu OS doesn't have similar system service to output coloured characters. You can use ANSI control codes to output to terminal, but I'm not sure how much work that is from assembly (if `write(...)` is enough, or not). Rather find 32 bit linux x86 assembly tutorial (ideally using NASM/YASM syntax), so you can focus on learning basics without fixing the example code, that's not *that* simple even for seasoned developer, like in this case, when the original does use BIOS functionality which is not so easily available under linux console. – Ped7g Apr 02 '18 at 12:36
  • 2
    NASM doesn't generate DOS EXE programs, but it can generate DOS COM programs. Use `nasm -f bin progname.asm -o progname.com`. You will need to add a `org 100h` to the top of the program and then use square brackets around `x`, `y`, `color` to reference the values at those memory locations.The lines would become `mov bl, [color]` `mov dl, [x]` and `mov dh, [y]` . You'll need to run this in a DOS emulator or VM running DOS to be usable. DOSBox would work. – Michael Petch Apr 02 '18 at 12:37

0 Answers0