News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

cmp between numbers

Started by lelejau, December 14, 2010, 06:16:29 PM

Previous topic - Next topic

lelejau

Well, I have this code:

Quote
.386
option casemap :none

include \masm32\include\masm32rt.inc
include \masm32\include\msvcrt.inc

includelib \masm32\lib\msvcrt.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

.data
certon db '5',0
texto db 'Adivinhe o numero',0
certo db 'Certo!',0
errado db 'Errado!',0
format db '%s',0
.data?

buffer db 1024 dup(?)

.code
 
 
Main proc
    invoke crt_printf,addr texto
    invoke crt_scanf,addr format, addr buffer

    mov eax,dword ptr [buffer]
    mov ecx,dword ptr [certon]

    .if eax==ecx
        push offset certo
        call crt_printf
        add esp,4h
    .else
        push offset errado
        call crt_printf
        add esp,4h
    .endif
   


    call crt__getch
    ret
Main endp
start:
    invoke Main
    invoke ExitProcess,0
END start
I have to type the correct number, that is in case 5.
And I want to cmp the number I entered in console with the certon variable.

But, I'm getting always the "Errado", which means, "Wrong".

What am I doing wrong?
Learning assembly :)

Tight_Coder_Ex

Almost right idea but using ECX brings in data beyond "certon"

    mov al,byte ptr [buffer]
    mov cl,byte ptr [certon]

    .if al==cl
        push offset certo
        call crt_printf
        add esp,4h
    .else
        push offset errado
        call crt_printf
        add esp,4h
    .endif



lelejau

why cl,al? what cl and al means?
Learning assembly :)

oex

These are BYTE registers

al,cl,dl,bl,ah,ch,dh,bh


|                |     al,ah       |
|                |       ax        |
|               eax                |


al/ah = BYTE  (8 bits)
ax = WORD    (16 bits)
eax = DWORD (32 bits)

etc
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

BogdanOntanu

Quote from: lelejau on December 14, 2010, 07:06:00 PM
why cl,al? what cl and al means?

AL is the lower 8 bits part of EAX
CL is the lower 8  bits part of ECX

You must pay attention to the size of your data structures or buffers.

For example you define "certon" with DB directive (define byte) hence you define an 8 bits data size.
If you read it in EAX/ECX then you are reading as an  32 bits data size.
But if  you read it in AL,CL then you read an 8 bits data size.

You could also use the more modern MOVZX instruction:

MOVZX EAX,byte ptr [buffer]
MOVZX ECX,byte ptr [certon]


This will extend unsigned data size from 8 bits to 32 bits (something like a "cast" in C). For signed extension you can use MOVSX.

FYI there are also some 16 bits parts to registers: AX, CX, DX, BX  are  16 bits parts and there is also an higher 8 bits part: AH, CH, DH, BH are 8 bits higher parts.



Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

lelejau

thank you. one more doubt, in printf how do I do "/n", I mean printf("new line/n"). the /n,\n doesnt work. what am I doing wrong?
Learning assembly :)

Tight_Coder_Ex

I'm not that familiar with console application but maybe try texto db 'Adivinhe o numero', 13, 10, 0 and don't bother with "\n".  13 carriage return and 10 is line feed.

BogdanOntanu

Quote from: lelejau on December 14, 2010, 07:37:08 PM
thank you. one more doubt, in printf how do I do "/n", I mean printf("new line/n"). the /n,\n doesnt work. what am I doing wrong?

You have to define them yourself, most assemblers do not understand C like escape codes for special characters.

For example

CR equ 13h
LF equ 10h
TAB equ 08h

my_string db CR,LF," This has a new line at start",TAB
        db "and then a tab",CR,LF,"and another new line...",0

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

oex

Carriage return (CR), ASCII code 13 (0D hex) is used to go back to the start of the line
Line feed (LF, sometimes newline or \n), ASCII code 10 (0A hex) is used to go to the next line

Linux uses just \n Windows tends to use both together this makes linux files not load properly in notepad for example :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

i know, for example, that if you want to generate a "\n" char for a message box, you can use just a linefeed (0Ah)
in console mode, however, a carriage return, then a linefeed is used
console mode is a bit tricky if you use the last character on the line because the console inserts the newline for you

lelejau

texto db 'Adivinhe o numero',13,10,0



worked, thanks.
Learning assembly :)

Tight_Coder_Ex

Eventhough hard coding works but this example modified to use your data

Quote from: BogdanOntanu on December 14, 2010, 07:43:53 PM

CR equ 13h
LF equ 10h
TAB equ 08h

texto db 'Adivinhe o numero', CR, LF, 0


is probably a better habit to get into as if your application need change for some reason then these values in an "INC" file would make it much easier.  However, 13 & 10 have been around since before I was born, so thier probably not going to change anytime soon.

dedndave

QuoteHowever, 13 & 10 have been around since before I was born...

well, i wouldn't go THAT far   :P
ASCII only goes back to the early 60's

for teletype (5-bit baudot), CR was 8 and LF was 2   :bg