Tuesday, May 10, 2011

The Postroom Computer Assignment solution

ADD:

%ADDLL msw1 lsw1 msw2 lsw2

MOV .R1 @&:msw1:
MOV .R0 @&:lsw1:
MOV .R3 @&:msw2:
MOV .R2 @&:lsw2:

ADD .R0 .R2
JMP &CRY :carrye: ;ADD lsw1 and lsw2 and if carry then GOTO carrye

:backy:
ADD .R1 .R3
JMP :outy: ; Add msw1 and msw2 and GOTO show output

:carrye:
ADD .R1 &1 ; Just add 1 the carry of lsw1 and lsw2 addition
JMP :backy: ; Go back to add Msw1 and MSW2

:outy:

OUT .R1 &USGN
MOV .R1 &9 ;initialise digit count
:loop: SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2
ADD .R2 &'0' ;add the ASCII code for 0
OUT .R2 &CHR ;output the digit
SUB .R1 &1 ;decrease digit count
JMP >Z :loop: ;if still digits to print repeat the loop

%


MASK:

%MSKL msw1 lsw1 msw2 lsw2

MOV .R1 @&:msw1:
MOV .R0 @&:lsw1:
MOV .R3 @&:msw2:
MOV .R2 @&:lsw2:

MSK .R1 .R3
MSK .R0 .R2

OUT .R1 &USGN

MOV .R1 &9 ;initialise digit count
:loop: SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2
ADD .R2 &'0' ;add the ASCII code for 0
OUT .R2 &CHR ;output the digit
SUB .R1 &1 ;decrease digit count
JMP >Z :loop: ;if still digits to print repeat the loop

%


SHIFT:

%SHFTL msw1 lsw1 shftamnt

MOV .R1 @&:msw1:
MOV .R0 @&:lsw1:
MOV .R3 @&:shftamnt:


MOV .R9 .R3
MOV .R5 &1
ADD .R5 .R9
JMP >Z :looppos: ; check either we want to go left or right


MOV .R5 &9
:loopneg: ; we got tough task right shift

MOV .R7 .R1
JMP :loopget: ; approch use it shift left 9 times to get the number which we have to transfer from msw to lsw
:loopret:

JMP &:multget: ; multiply tht number with 100000000 and add in the lsw to make proper right shift simple as that
:done:
SHF .R0 @&:amount:
ADD .R0 .R8
SHF .R1 @&:amount:
MOV .R5 &9
ADD .R9 &1
JMP <Z :loopneg: ; wait untill all 9 digits are properly transfered
JMP :outy:

:looppos:
SHF .R1 &1 ;shift number one place to the left
SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2 and place it in the msw by simply adding as msw is already shifted and zero in decimal place
ADD .R1 .R2
SUB .R9 &1
JMP >Z :looppos:
JMP :outy: ; display output


:outy:
OUT .R1 &USGN
MOV .R1 &9 ;initialise digit count
:loop: SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2
ADD .R2 &'0' ;add the ASCII code for 0
OUT .R2 &CHR ;output the digit
SUB .R1 &1 ;decrease digit count
JMP >Z :loop: ;if still digits to print repeat the loop

JMP :endy:


:loopget: ; loop to get the decimal from msw which needs to be transfered in lsw
SHF .R7 &1
MOV .R8 .CAR ; can also be achieved in single 9 shifts
SUB .R5 &1
JMP >Z :loopget:
JMP :loopret:



:multget: ; multiply only by shifting 8 times
SHF .R8 &8
JMP &:done:


:amount: (-1)

:endy:
%


SUB:

%SUBLL msw1 lsw1 msw2 lsw2

MOV .R1 @&:msw1:
MOV .R0 @&:lsw1:
MOV .R3 @&:msw2:
MOV .R2 @&:lsw2:

SUB .R1 .R3 ;sub msw1 msw2
JMP >Z :firstpos: ; if the answer is pos then our net answer is pos as well
JMP &EQZ :firstzer: ; if first answer is zero thn use this jum

SUB .R0 .R2 ; Sub lsw1 lsw2 you will only come here if result of sub msw1 msw2 was neg
JMP >Z :firstnegsecpos: ; Sub lsw1 lsw2 is pos number thn use this
JMP <Z :firstnegsecneg: ; Sub lsw1 lsw2 is neg number thn use this
JMP :outy2:

:firstnegsecneg: ; both individual sub resulted in neg our over all result is neg
MOV .R6 .R0 ; we have to now get our correct result
MOV .R8 &0 ; approch used it just convert the sec neg sub lsw1 lsw2 to pos
:loopy:
ADD .R8 &1
ADD .R6 &1 ; use loop to convert neg to pos add one and count until neg becomes zero
JMP <Z :loopy:
MOV .R0 .R8
JMP :outy2: ; and print output use outy2 if you have answer neg


:firstnegsecpos:
MOV .R9 @&:mx: ; sub msw1 msw2 was neg while sub lsw1 lsw2 was pos
SUB .R9 .R0
MOV .R0 .R9 ; approch is minus sec neg from max possible number use scrap paper to verify
ADD .R0 &1 ; add 1 to compensate the result over all result neg
JMP :outy2:

:firstpos: ; result shud be pos as sub msw1 msw2 is pos
SUB .R0 .R2
JMP >Z :outy1:
JMP &EQZ :outy1:

:firstpossecneg:
MOV .R9 @&:mx: ; sub msw1 msw2 is pos so result pos
ADD .R0 .R9
ADD .R0 &1 ; add neg number to max pos number and get value
SUB .R1 &1 ; sub the carry u took use scrap paer to verify if u dnt undersatnd
JMP :outy1:

:firstzer:
SUB .R0 .R2 ; first was zero i.e. sub msw1 msw2 so ans is simple sub lsw1 lsw2
JMP :outy1:


:outy1:
OUT .R1 &USGN
MOV .R1 &9 ;initialise digit count
:loop: SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2
ADD .R2 &'0' ;add the ASCII code for 0
OUT .R2 &CHR ;output the digit
SUB .R1 &1 ;decrease digit count
JMP >Z :loop: ;if still digits to print repeat the loop
JMP &LWY :end:


:outy2:
OUT .R1 &SGN
MOV .R1 &9 ;initialise digit count
:loop: SHF .R0 &1 ;shift number one place to the left
MOV .R2 .CAR ;copy the carry to R2
ADD .R2 &'0' ;add the ASCII code for 0
OUT .R2 &CHR ;output the digit
SUB .R1 &1 ;decrease digit count
JMP >Z :loop: ;if still digits to print repeat the loop
JMP &LWY :end:

:mx: (999999999)


:end:
%

No comments:

Post a Comment