wouldnotliketo
wouldnotliketo

Reputation: 323

Multiplying hexadecimal numbers in nasm

A short version: how do I multiply hexadecimal numbers with asm, where will the result be stored and how do I access it and move it to the eax register?

A longer version, which explains why would I need to do that and in which format:

I'd like to have a byte array which stores ASCII codes of some characters I'm going to output into the video memory. I can output two characters like this:

org 100 h

start:
    mov ax, 0003h
    int 10h

    mov ax, 0b800h
    mov es, ax
    mov di, 100
    mov eax, 1f651f48h
    stosd

    ret

This will output He in white letters on blue background to somewhere around the upper left corner of the screen. But I'd like to print 'Hello world!'. So I thought I'd store symbol codes in an array, form hexadecimal numbers like 1f651f48h by addition and multiplication and output them by using rep command for a loop.

But I don't understand how to form such hexadecimal numbers. I don't really understand if the numbers I'm using here would need to be in eax or ax, or how do I fetch the result. So I'd like to request some explanations.

How this number works: 1f is an attribute, this is for white letters on blue background. 65 is the ASCII code for e, 48 is for h. So I'd need to multiply 48 by 10000 (all numbers are hexadecimal) and add to 1f001f00h, then add 65 to the result of the previous actions and move it all to the eax register.

I'm also confused about the use of the registers. The cx register is used for, so to say, 'loop iterations'; bx will be used for iterating over the array; the resulting number needs to be put in eax. This leaves me with nothing but edx for interim calculations, is it possible to do the things I need with only this?

I'm using NASM under a freedos emulation in qemu.

Upvotes: 2

Views: 1719

Answers (1)

Sep Roland
Sep Roland

Reputation: 39256

How this number works: 1f is an attribute, this is for white letters on blue background. 65 is the ASCII code for e, 48 is for h. So I'd need to multiply 48 by 10000 (all numbers are hexadecimal) and add to 1f001f00h, then add 65 to the result of the previous actions and move it all to the eax register.

If you were to follow this calculation you'd see on screen a WhiteOnBlue "eH", not the "He" that you expect!

The good news here is that you don't need any calculations to display the text.
Just define the message and then in a loop get a single character from it and put it on the screen.
Since this is a .COM file (org 100h), the DS segment register is already setup correctly.

org 100h

start:
    mov ax, 0003h  ;Text video mode 80x25
    int 10h

    mov ax, 0B800h ;Segment of video memory
    mov es, ax
    mov di, 100    ;Address in video memory
    cld        
    mov si, msg    ;Where is the message
Again:
    lodsb          ;Get 1 character into AL
    cmp al, 0      ;Is it terminating zero?
    je  EndOfLoop  ;Yes
    mov ah,1Fh     ;Combine with attribute
    stosw          ;Store AX in video memory
    jmp Again      ;Continu the loop
EndOfLoop:
    ret

msg db 'Hello world!',0

Upvotes: 1

Related Questions