0

I've just tried to write my first program in assembly for x86 and I don't know why, but it doesn't make what I want. There's no errors, no communicates but the program doesn't open after pushing 'execute'. i want these program to add two variables and send back theirs sum. here's code:

    .386
.model flat, stdcall
option casemap:none

include windows.inc
include user32.inc
include kernel32.inc

includelib user32.lib
includelib kernel32.lib

.data
a dw 1h
b dw 2h
z dw 01 dup(?),0
.data?

.code
start:
    mov ds, ax
    mov ax, a
    mov bx, b
    clc
    add ax, bx
    mov edi, offset z
    mov [edi], ax

    mov ah, 09h
    mov dx, z
    int 21h

    mov ah, 4ch
    int 21h

end start

Please, help me :C

  • It's not clear if you're trying to compile this as 16bit code. (if so why is there a EDI in the middle?). What you wrote looks like old DOS-era assembly, it wouldn't run on any modern system. I suggest you look at more recent tutorials/books if you're trying to learn modern assembly. – tux3 Jan 08 '15 at 18:24
  • You seem to be buliding a Windows executable, but the code is for DOS. That's not going to work. Find a tutorial for Windows assembly instead (IIRC MASM32 includes lots of example code). – Michael Jan 08 '15 at 18:24
  • Note that DOSBox should still be able to run this, if you insist on using DOS interrupts. – tux3 Jan 08 '15 at 18:25
  • Not if assembled and linked as-is. Wrong memory model, and linking against user32.lib and kernel32.lib suggests PE as the output format, rather than a DOS-style MZ-executable. – Michael Jan 08 '15 at 18:28
  • Hi, unfortunately I don't know which tutorial/book I should choose. Could you help me and recommend something? – Dominik Jarzemski Jan 08 '15 at 18:28
  • If you can comment your code, it will help a lot; generally one comment per instruction, unless the instruction is obscure and needs two or three comments to help the reader understand it. – User.1 Jan 08 '15 at 18:48

1 Answers1

0

DOS function 09h expects a string; specifically, an array of bytes, each containing the ASCII code of a character, terminated by 24h (the ASCII code of $). Example:

z   db '3$'

which is equivalent with:

z   db 33h, 24h

Instead, you have defined z as an array of words, and fill the first word with 03h (the result of 1+2). In ASCII, 03h is a non-printing character.

Assembly is not some high-level language with convenient automatic type conversions. In assembly, you will have to convert the numeric value into a sequence of ASCII characters yourself.

How to do this? That has been asked numerous times already. Like here: Assembly, printing ascii number

Unless of course you have a convenient library with conversion functions lying around.

Community
  • 1
  • 1
Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45