flat assembler
Message board for the users of flat assembler.

Index > Main > Having problems verifying my program is working.

Author
Thread Post new topic Reply to topic
Walkerboh



Joined: 08 Jul 2010
Posts: 17
Walkerboh 16 Aug 2010, 21:42
I'm working on this programming project from Art Of Assembly chapter 1. I know that the ascii char to int code is working correctly and I know that I'm packing the dates correctly. When I try to verify that my date unpacking procedures are working properly is where i run into problems. I'm checking three different unpack procedures but only getting feedback from one? If I test the unpack procedures one at a time, the ExtractYear procedure when run alone crashes my program "Illegal Instruction".

So am I unpacking the dates incorrectly or am i testing the procedures in a faulty manner? Thx in advance, below is the programming project and my code.

Programming Project:
"Write a ToDate function that accepts three parameters, a month, day, and year value. This function should return the 16-bit packed date value using the format given in this chapter (see “Bit Fields and Packed Data” on page 28 ). Write three corresponding functions ExtractMonth, ExtractDay, and ExtractYear that expect a 16-bit date value and return the corresponding month, day, or year value. The ToDate function should automatically convert dates in the range 1900-1999 to the range 0..99."

Code:
org 100h
use16
   xor     ax, ax

  call    PackDate

        call    ExtractDay
  cmp     al, 31
      je      ProcessWorked
       jne     ProcessFailure
      
    call    ExtractMonth
        cmp     al, 12
      je      ProcessWorked
       jne     ProcessFailure

  call    ExtractYear
 cmp     al, 99
      je      ProcessWorked
       jne     ProcessFailure

  

EndProgram:
     mov     ah, 4ch
     int     21h



ExtractDay:
;UnPacks the day from PackedDate and returns value into al
    pusha
       xor     ax, ax
      mov     [UnPacker], al
      mov     ax, [PackedDate]
    and     ax, 0000000000011111b
       mov     [UnPacker], al
      popa
        mov     al, [UnPacker]
      ret
ExtractMonth:
;UnPacks the month from PackedDate and returns value into al
    pusha
       xor     ax, ax
      mov     [UnPacker], al
      mov     ax, [PackedDate]
    ror     ax, 5
       and     ax, 0000000000001111b
       mov     [UnPacker], al
      popa
        mov     al, [UnPacker]
      ret

ExtractYear:
;UnPacks the year from PackedDate and returns value into al
  xor     ax, ax
      mov     [UnPacker], al
      mov     ax, [PackedDate]
    ror     ax, 9
       and     ax, 0000000001111111b
       mov     [UnPacker], al
      popa
        mov     al, [UnPacker]
      ret

CharStrToInt:
;SI = 5 byte string of ascii chars(30h-39h) to convert to an int
;Requires word memory slot(IntValue) to hold temporary data
;Returns int in cx
       pusha
       mov     cx, 5
       mov     [IntValue], 0
   ConvertLoop:
        lodsb
       and     ax, 00001111b           ;Zero out H.O. nibble of ascii char
 
    cmp     cx, 5                   ;If placeholder is 5 then mul value by 10,000
       jne     @f
  mov     bx, 10000
   mul     bx
     @@:cmp   cx, 4                   ;If placeholder is 4 then mul value by 1,000
        jne     @f
  mov     bx, 1000
    mul     bx
     @@:cmp   cx, 3                   ;If placeholder is 3 then mul value by 100
  jne     @f
  mov     bx, 100
     mul     bx
     @@:cmp   cx, 2                   ;If placeholder is 2 then mul value by 10
   jne     @f
  mov     bx, 10
      mul     bx
     @@:add   [IntValue], ax          ;Add converted value to IntValue
    dec     cx                      
    cmp     cx, 0
       jnz     ConvertLoop             ;Loop until cx = 0
  popa
        mov     cx, [IntValue]
      ret

PackDate:
;Requires word var(PackedDate) to place packed value into
;Also requires three 5 byte strings(Day/Month/Year) to pack into PackedDate
;Date is packed into 16 bits bits 15-9 for year, bits 8-5 month, bits 0-4 day
       pusha
       xor     cx, cx
      xor     ax, ax  

        mov     si, Day         ;Pack Day into 16bit ax
     call    CharStrToInt
        or      ax, cx


      mov     si, Month       ;Pack Month into 16bit ax
   call    CharStrToInt
        ror     ax, 5
       or      ax, cx

  mov     si, Year        ;Pack Year into 16bit ax
    call    CharStrToInt
        ror     ax, 4
       or      ax, cx

  ror     ax, 7           ;Adjust so value is in Year/Month/Day format    
    mov     [PackedDate], ax        
    popa
        ret

ProcessWorked:
       pusha
       mov     ah, 09h
     mov     dx, PrintSuccess
    int     21h
 popa
        ret

ProcessFailure:
      pusha   
    mov     ah, 09h
     mov     dx, PrintFailure
    int     21h
 popa
        ret

Day          db      '00031'
Month              db      '00012'
Year               db      '00099'
PackedDate dw      ?
UnPacker   db      ?
CharString db      '65535'
IntValue   dw      ?
PrintSuccess       db 'Conversion complete!',13,10,'$'
PrintFailure db 'Process Failed!!!!!',13,10','$'    
Post 16 Aug 2010, 21:42
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 16 Aug 2010, 23:32
ProcessWorked and ProcessFailure need to be "call"ed, not jumped to. ExtractYear fails because you forget the "pusha".


Here's the fixed code, it outputs three "Conversion complete!"s. I kind of uglified the start with my fix, though. Smile
Code:
org 100h
use16
        xor     ax, ax

        call    PackDate

        call    ExtractDay
        cmp     al, 31
        je      daysuccess
        call    ProcessFailure
        jmp     @f
     daysuccess:
        call    ProcessWorked
     @@:
        
        call    ExtractMonth
        cmp     al, 12
        je     monthsuccess
        call    ProcessFailure
        jmp     @f
     monthsuccess:
        call    ProcessWorked
     @@:

        call    ExtractYear
        cmp     al, 99
        je      yearsuccess
        call    ProcessFailure
        jmp     @f
     yearsuccess:
        call    ProcessWorked
     @@:

        

EndProgram:
        mov     ah, 4ch
        int     21h



ExtractDay:
;UnPacks the day from PackedDate and returns value into al
        pusha
        xor     ax, ax
        mov     [UnPacker], al
        mov     ax, [PackedDate]
        and     ax, 0000000000011111b
        mov     [UnPacker], al
        popa
        mov     al, [UnPacker]
        ret
ExtractMonth:
;UnPacks the month from PackedDate and returns value into al
        pusha
        xor     ax, ax
        mov     [UnPacker], al
        mov     ax, [PackedDate]
        ror     ax, 5
        and     ax, 0000000000001111b
        mov     [UnPacker], al
        popa
        mov     al, [UnPacker]
        ret

ExtractYear:
;UnPacks the year from PackedDate and returns value into al
        pusha
        xor     ax, ax
        mov     [UnPacker], al
        mov     ax, [PackedDate]
        ror     ax, 9
        and     ax, 0000000001111111b
        mov     [UnPacker], al
        popa
        mov     al, [UnPacker]
        ret

CharStrToInt:
;SI = 5 byte string of ascii chars(30h-39h) to convert to an int
;Requires word memory slot(IntValue) to hold temporary data
;Returns int in cx
        pusha
        mov     cx, 5
        mov     [IntValue], 0
   ConvertLoop:
        lodsb
        and     ax, 00001111b           ;Zero out H.O. nibble of ascii char
        
        cmp     cx, 5                   ;If placeholder is 5 then mul value by 10,000
        jne     @f
        mov     bx, 10000
        mul     bx
     @@:cmp     cx, 4                   ;If placeholder is 4 then mul value by 1,000
        jne     @f
        mov     bx, 1000
        mul     bx
     @@:cmp     cx, 3                   ;If placeholder is 3 then mul value by 100
        jne     @f
        mov     bx, 100
        mul     bx
     @@:cmp     cx, 2                   ;If placeholder is 2 then mul value by 10
        jne     @f
        mov     bx, 10
        mul     bx
     @@:add     [IntValue], ax          ;Add converted value to IntValue
        dec     cx                      
        cmp     cx, 0
        jnz     ConvertLoop             ;Loop until cx = 0
        popa
        mov     cx, [IntValue]
        ret

PackDate:
;Requires word var(PackedDate) to place packed value into
;Also requires three 5 byte strings(Day/Month/Year) to pack into PackedDate
;Date is packed into 16 bits bits 15-9 for year, bits 8-5 month, bits 0-4 day
        pusha
        xor     cx, cx
        xor     ax, ax  

        mov     si, Day         ;Pack Day into 16bit ax
        call    CharStrToInt
        or      ax, cx


        mov     si, Month       ;Pack Month into 16bit ax
        call    CharStrToInt
        ror     ax, 5
        or      ax, cx

        mov     si, Year        ;Pack Year into 16bit ax
        call    CharStrToInt
        ror     ax, 4
        or      ax, cx

        ror     ax, 7           ;Adjust so value is in Year/Month/Day format    
        mov     [PackedDate], ax        
        popa
        ret

ProcessWorked:
        pusha
        mov     ah, 09h
        mov     dx, PrintSuccess
        int     21h
        popa
        ret

ProcessFailure:
        pusha   
        mov     ah, 09h
        mov     dx, PrintFailure
        int     21h
        popa
        ret

Day             db      '00031'
Month           db      '00012'
Year            db      '00099'
PackedDate      dw      ?
UnPacker        db      ?
CharString      db      '65535'
IntValue        dw      ?
PrintSuccess    db 'Conversion complete!',13,10,'$'
PrintFailure    db 'Process Failed!!!!!',13,10','$'
    






EDIT:
Ok, technically they don't need to be "call"ed. Something like this would work:
Code:
        call    ExtractDay
        push @f
        cmp     al, 31
        je      ProcessWorked
        jne     ProcessFailure
     @@:
        
        call    ExtractMonth
        push @f
        cmp     al, 12
        je      ProcessWorked
        jne     ProcessFailure
     @@:

        call    ExtractYear
        push @f
        cmp     al, 99
        je      ProcessWorked
        jne     ProcessFailure
     @@:   
    

Yeah, that looks a lot better.

_________________
----> * <---- My star, won HERE
Post 16 Aug 2010, 23:32
View user's profile Send private message Reply with quote
Walkerboh



Joined: 08 Jul 2010
Posts: 17
Walkerboh 17 Aug 2010, 00:36
ah man, facepalm! Thanks windwakr Smile
Post 17 Aug 2010, 00:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.