70 likes | 215 Views
Programming Exercise #1 Write a program to add the contents of R0 and R1, and indicate in R2 if there was an overflow Flow Diagram Assembly Code. Programming Exercise #1. ; Add R3=R0+R1, R2=0 indicates no overflow ; .ORIG x3000
E N D
Programming Exercise #1 Write a program to add the contents of R0 and R1, and indicate in R2 if there was an overflow • Flow Diagram • Assembly Code
Programming Exercise #1 ; Add R3=R0+R1, R2=0 indicates no overflow ; .ORIG x3000 AND R2, R2, #0 ;Initially R2=0 (no Overflow assumed) ADD R3, R0, R1 ;R3=R0+R1 ; test for overflow ADD R0, R0, #0 ;test R0 BRN NEG ;Branch if RO negative ADD R1, R1, #0 ;test R1 BRN DONE ;No overflow if operand signs differ (R1 NEG) ADD R3, R3, #0 ;maybe, test R3 BRZP DONE ;No overflow if result sign matches (All POS) ADD R2, R2, #1 ;R2=1 indicating overflow NEG ADD R1, R1, #0 ;test R1 BRZP DONE ;No overflow if operand signs differ (R1 POS) ADD R3, R3, #0 ;maybe, test R3 BRN DONE ;No overflow if result sign matches (All NEG) ADD R2, R2, #1 ;R2=1 indicating overflow DONE HALT .END
Programming Exercise #2 Write a program to count the 1’s in register R0 • Flow Diagram • Assembly code
Programming Exercise #2 ; Program to count 1's in Register R0 ; R3 is a working copy of R0 ; R1 contains the count ; R2 is a loop counter .orig x3100 ADD R3, R0, #0 ;copy R0 into R3 AND R1, R1, #0 ;clear count ADD R3, R3, #0 ;test for Neg BRZP NEXT ;count if Neg ADD R1, R1, #1 NEXT AND R2, R2, #0 ;check remaining 15 bits ADD R2, R2, #-15 LOOP ADD R3, R3, R3 ;shift R3 left BRZP AGAIN ;count if Neg ADD R1, R1, #1 AGAIN ADD R2, R2, #1 ;loop until done BRN LOOP HALT .END
Programming Exercise #3 Write a program to add two, two digit numbers read from the console • Flow Diagram • Assembly code
; Program to add two 2 digit decimal numbers read from the console ; R1 & R2 are working registers to load 2 digit number ; R3 is first number ; R4 is second number ; R5 is the sum ; R6 is conversion offset .orig x3600 LEA R0, MESSAGE ;print message PUTS ; Get first number LD R0, NEWLINE ;print PROMPT1 OUT OUT LEA R0, PROMPT1 PUTS GETC ;get first character OUT LD R6, M30 ;convert char to hex ADD R0, R0, R6 ADD R1, R0, R0 ;R1 = 2xR0 ADD R2, R1, #0 ;copy R1 into R2 ADD R2, R2, R2 ;R2 = 4xR0 ADD R2, R2, R2 ;R2 = 8xR0 ADD R2, R2, R1 ;R2 = 10xR0 GETC ;get second character OUT ADD R0, R0, R6 ;convert to hex ADD R3, R2, R0 ;R3 = first decimal number ; Get second number LEA R0, PROMPT2 ;get first character PUTS GETC OUT ADD R0, R0, R6 ;convert char to hex ADD R1, R0, R0 ;R1 = 2xR0 ADD R2, R1, #0 ;copy R1 into R2 ADD R2, R2, R2 ;R2 = 4xR0 ADD R2, R2, R2 ;R2 = 8xR0 ADD R2, R2, R1 ;R2 = 10xR0 GETC ;get second character OUT ADD R0, R0, R6 ;convert to hex ADD R4, R2, R0 ;R4 = first decimal number Programming Exercise #3
; Add the numbers and print results ADD R5, R4, R3 ;R5 = R3 + R4 LEA R0, SUM ;prepare to print results PUTS LD R4, P100 ;find 1st digit LD R3, M100 AND R0, R0, #0 LOOP1 ADD R0, R0, #1 ADD R5, R5, R3 ;subtract 100 until negative BRZP LOOP1 ADD R5, R5, R4 ADD R0, R0, #-1 LD R6, P30 ;convert to ascii & print ADD R0, R0, R6 OUT AND R0, R0, #0 ;find 2nd digit LOOP2 ADD R0, R0, #1 ADD R5, R5, #-10 ;subtract 10 until negative BRZP LOOP2 ADD R5, R5, #10 ADD R0, R0, #-1 LD R6, P30 ;convert to ascii & print ADD R0, R0, R6 OUT ADD R0, R5, R6 ;convert and print 3rd digit OUT LD R0, NEWLINE OUT HALT MESSAGE .STRINGZ "Enter two 2-digit decimal numbers:" NEWLINE .FILL x000A PROMPT1 .STRINGZ " The sum of " PROMPT2 .STRINGZ " and " SUM .STRINGZ " is " M30 .FILL xFFD0 ;-x30 P30 .FILL X0030 ; x30 M100 .FILL xFF9C ;-100 P100 .FILL x0064 ; 100 .END Programming Exercise #3 (2)