Help! Printing in console (I get an error A2022)

Started by Seed_87, July 20, 2008, 05:17:09 PM

Previous topic - Next topic

Seed_87

Hello.. total newbie here!  :bg

So my problem is, the code for outputting to the cosole is the one below, only when I compile it, it throws this error:

" ERROR A2022: Instruction operands must be of the same size  "

Oh, I hope im not asking too much but: Outputting a number? Is it the same code?




.486                                        ; IS THIS OK? or is it .286 ?
    .model flat, stdcall                     
    option casemap :none               
    include \masm32\include\windows.inc     
    include \masm32\macros\macros.asm       
    include \masm32\include\kernel32.inc


.data                       
                     
    msg   db "this is the string to print",13,10,"$"


  .code                       ; Aqui comienza el codigo como tal...

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                              ; The CODE entry point to the program


     mov  dx, offset msg      ; dx register gets location of string to be printed

     mov  ah,09h        ; function code goes into the ah register

     int  21h           ; tell DOS to print the string


   exit

end start                       ; Tell MASM where the program ends






Mark Jones

Hello there and welcome to the forum. :U

Quote from: Seed_87 on July 20, 2008, 05:17:09 PM

.486                                        ; IS THIS OK? or is it .286 ?
    .model flat, stdcall                     
    option casemap :none               
    include \masm32\include\windows.inc     
    include \masm32\macros\macros.asm       
    include \masm32\include\kernel32.inc


The "flat" memory model is the windows 32-bit model. The DOS model is "small." Including masm32-anything is incompatible with DOS (DOS is a 16-bit operating system.)

MASM.EXE will assemble 16-bit instructions, but you must use the correct .model and includes, as well as a 16-bit linker (which does not come with the MASM32 package.) Note, many may suggest that learning 16-bit DOS programming is harder than learning 32-bit Windows programming, so if you only want to "start at the beginning to learn Windows programming," then learning DOS programming is an unnecessary step.

If you must learn DOS assembler, check out: http://www.btinternet.com/~btketman/tutpage.html
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Seed_87

Woah mark jones thanx for that link (ketman)  :bg
Actually i didnt know i was programming in either (16 or 32 bits), this is so new to me i just tried and saw if i could get it to work, however it did.

So, i think like everybody when they first learn how to program in some language, i tried working on some code i got off some tutorial.. and so got things to work.

I need to seriously arrange my code so that its 32bits. since its apparently easier. Thanx Mark Jones again!  :wink :U

MichaelW

It might be helpful for you to know why the assembler returned the error. The instruction:

mov dx, offset msg

Is attempting to copy the offset address of msg into the 16-bit register DX. In 16-bit code an offset address is 16 bits, so it would fit in a 16-bit register OK. In 32-bit code an offset address is 32 bits, which obviously will not fit in a 16-bit register.

A minimal and workable (assuming you used a 16-bit linker) 16-bit version of your code, using the small memory model, could be like this:

.model small
.stack 
.data
    msg db "this is the string to print",13,10,"$"
.code
.startup
    mov dx, offset msg
    mov ah, 09h
    int 21h
.exit
end


A minimal and workable 32-bit version of your code, using the MASM32 package, could be like this:

include \masm32\include\masm32rt.inc
.data
  msg db "this is the string to print",13,10,0
.code
start:
    print ADDR msg
    exit
end start

eschew obfuscation