The MASM Forum

General => The Campus => Topic started by: sam on November 19, 2020, 09:19:21 PM

Title: Debug and help please
Post by: sam on November 19, 2020, 09:19:21 PM
CS216 in the Microsoft Macro Assembler (MASM) assembler for writing x86 assembly code.

;  The CS240 ISBN-10 Checking Program
;
;  Student:
;
;  This program validates the ISBN-10 check digit.
;
;  Please fix it.
;
; This program runs in SASM
;
;------------------------------------------------------------------
;
%include "io.inc"

section   .data

here   db   "I am here",10,0

msg1   db   "Computed checksum=",0
msg2   db   " Remainder=",0

isbn1   db   "020161262X",0   ; 10 digit ISBN
isbn2   db   "020161622X",0   ; 10 digit ISBN
isbn3   db   "1814462082",0   ; 10 digit ISBN

okmsg   db   ' '      ; ISBN is followed by Space, then 'Y'/'N'
okflag   db   ' '      ; Put 'Y' or 'N' here
   db   0x0a      ; Newline
   db   '0'      ; terminating zero


section   .text

;  Main Program
;
   global CMAIN
CMAIN:
        mov ebp, esp; for correct debugging
   
; Test the three ISBNs
   mov   esi,isbn3   ; Test ISBN 3
   call   isbntest         ;isbntest(isbn3)  call subroutine with this argument
   mov   esi,isbn1   ; Test ISBN 1
   call   isbntest
   mov   esi,isbn2   ; Test ISBN 2
   call   isbntest
;
;
        xor eax,eax
   ret

;-----------------------
;  ISBNTEST Subroutine
;  Enter with ESI = address of leftmost byte of zero-terminated 10 digit ascii ISBN.
;  This will print the isbn followed by Y or N indicating OK or not.
;  It will also print the computed checksum and remainder.
;-----------------------
isbntest:
   push   ecx      ; Save registers we will need
   push   ebx
   mov   ecx,10      ; ECX position number of digit being added up
            ; ESI Address of ISBN ascii string
   mov   eax,0      ; EBX Result
;
;  Get here for every loop
;
nextdig:
   ;; debugging  Move this to various points in the program for a debugging write.
        PRINT_STRING here
   ;; end debugging

   mov   al,[esi]   ; al next ascii byte
   cmp   eax,'X'      ;
   jne   isnotx      ; Digit 'X'
   mov   al, 10           ;   has value 10
   jmp   havenum      ;
isnotx:
   and   esi,dword 0x0f   ; Turn digit '0' to '9' into number
havenum:
   mul   ecx      ; EDX:EAX = digit * position
   add   eax,ebx      ; EBX=sum result (edx=0, product fits in eax)
   dec   esi      ; Point to next byte
        dec      ecx
        jnz     nextdig
;   loop   nextdig          ; equivalent to:  dec ecx,   jnz nextdig

   mov   eax,0
   mov   edx,ebx      ; edx:eax = sum
   mov   ecx,11      ; divide by 11
   div   ecx      ; EAX = Remainder mod 11 (EDX=quotient)
   mov   al,'Y'      ; If remainder is zero print 'Y' (OK)
   sub   edx,edx
   jz   prt
   mov   al,'N'      ; If result is nonzero print 'N'
prt:
   mov   al,[okflag]   ; Put result in message
   sub   esi,10      ; Restore address of input ISBN
   PRINT_STRING [esi]     ; print it
   PRINT_STRING okmsg        ; print OK message
        PRINT_STRING okflag     ; print the result Y or N
;
; Now print the checksum and remainder for debugging purposes
;
      
   PRINT_STRING msg1      ; "Checksum=" message
   mov   eax,eax      ; Value of checksum
   PRINT_DEC  4,eax
   PRINT_STRING msg2   ; " Remainder=" message
   PRINT_DEC 4,[edx]          ; Value of remainder
        NEWLINE
        NEWLINE
;
   popa
   ret
Here is the output of the working program:
1814462082 Y
Computed checksum=198 Remainder=0
020161262X N
Computed checksum=106 Remainder=7
020161622X Y
Computed checksum=110 Remainder=0
Title: Re: Debug and help please
Post by: hutch-- on November 19, 2020, 10:00:41 PM
Perhaps you could tell us what assembler you are using and what technical base.

As SASM looks like a typo for MASM and the code looks like 32 bit, make us wiser. The called routines have no known origin unless they are Irvine book procedures.
Title: Re: Debug and help please
Post by: Vortex on November 19, 2020, 10:54:03 PM
Hi sam,

Welcome to the forum.

Are you using this IDE?

QuoteSASM

Simple crossplatform IDE for NASM, MASM, GAS, FASM assembly

https://dman95.github.io/SASM/english.html
Title: Re: Debug and help please
Post by: sam on November 20, 2020, 02:46:41 AM
Quote from: hutch-- on November 19, 2020, 10:00:41 PM
Perhaps you could tell us what assembler you are using and what technical base.

As SASM looks like a typo for MASM and the code looks like 32 bit, make us wiser. The called routines have no known origin unless they are Irvine book procedures.

32-bit x86 assembly language programming,CS216  the Microsoft Macro Assembler (MASM) assembler. MASM uses the standard Intel syntax for writing x86 assembly code.
Title: Re: Debug and help please
Post by: HSE on November 20, 2020, 03:21:53 AM
Quote from: sam on November 19, 2020, 09:19:21 PM
; This program runs in SASM

%include "io.inc"

section   .data

SASM is an IDE apparently thinked for NASM, and you need that io.inc for NASM.
Title: Re: Debug and help please
Post by: deeR44 on March 26, 2022, 02:24:15 PM
Quote32-bit x86 assembly language programming,CS216  the Microsoft Macro Assembler (MASM) assembler. MASM uses the standard Intel syntax for writing x86 assembly code.
Thank you, SAM! I had no idea that 32-bit x86 assembly language was "CS216", whatever that is.

Thank you, too, for the information that Microsoft's Macro Assembler uses the standard Intel syntax for writing
x86 assembly code. I had no idea of any of this valuable information. I shall remember it.
Title: Re: Debug and help please
Post by: mikeburr on March 27, 2022, 10:04:49 AM
dont be so sarcastic deer44.. your supposed to welcoming new AI guided Bots the the forum
regards mike b
Title: Re: Debug and help please
Post by: deeR44 on March 28, 2022, 02:30:18 PM
Quotedont be so sarcastic deer44.. your supposed to welcoming new AI guided Bots the the forum
regards mike b
Ok, Mike.

Title: Re: Debug and help please
Post by: hutch-- on March 28, 2022, 05:35:31 PM
He sounds more like a student than a bot.  :tongue:
Title: Re: Debug and help please
Post by: deeR44 on March 29, 2022, 02:46:32 PM
QuoteHe sounds more like a student than a bot.
Who Hutch? Mike, Sam, or me? Probably not me, most people who know me think I'm too stupid to be a student.
Title: Re: Debug and help please
Post by: hutch-- on March 29, 2022, 08:29:13 PM
 :biggrin:

No, it was a response to Mike's comment.

> I'm too stupid to be a student

You may find you are not stupid enough to be a student.  :tongue:
Title: Re: Debug and help please
Post by: NoCforMe on July 21, 2022, 08:40:28 AM
While everyone else was getting all snarky and stuff about the OP here*, I think I might have found an error (maybe not the error):


isbn3   db   "1814462082",0   ; 10 digit ISBN

; Test the three ISBNs
   mov   esi,isbn3   ; Test ISBN 3


Seems to me that this ought to be

   lea   esi,isbn3   ; Test ISBN 3


or

   mov   esi, OFFSET isbn3   ; Test ISBN 3


* Who's obviously a student taking the computer science course CS216.