2013-07-14 144 views
0

我似乎無法計算此代碼中的正常工資。它只是計算加班時間的比率而不是包括正常工資在內的全部工資。 Here是OAMulator編譯器的鏈接。OAM彙編代碼錯誤

SET 40  #put the value 40 into the accumulator 
STA 50 #store the value 40 in address 50 (I chose 50 arbitrarily) 
SET 1.5 #put the value 1.5 into the accumulator 
STA 51 #store the value 1.5 in address 51 
LDA 0  #input the number of hours worked 
STA 52 #store the number of hours worked in address 52 
LDA 0  #input the payrate 
STA 53 #store the payrate at address 53 
LDA 52 #load the number of hours worked into the accumulator 
SUB 50 #subtract the number 40 (stored in address 50) 
STA 54 #store overtime hours at address 54 
BRP RT #if hours is more than 40, branch to overtime 
LDA 50 #load 40 into the accumulator 
MLT 53 #multiply by the payrate. 
STA 55 #store REGULAR PAY pay in address 55 
RT, LDA 52 #load the number of hours worked 
LDA 54 #overtime hours 
MLT 51 #overtime rate 
MLT 53 #payrate 
ADD 55 #add the regular pay 
STA 56 #all of the pay 
BR FINAL #branch to where you are going to print the week's pay 
STA 56 #store total pay including regular pay and overtime pay in address 56 
FINAL,STA 0 #output final pay 
HLT 

這裏有什麼問題,我該如何解決?

+0

當計算加班時間時,程序不會輸出總工資。我似乎無法弄清楚錯誤在哪裏。 – user2548833

回答

0
BRP RT #if hours is more than 40, branch to overtime 

這個分支跳躍過去,如果工作小時數超過40。所以,你現在在做什麼是這樣計算的工資正常代碼:

if (overtime_hours <= 0) { 
    regular_pay = 40 * pay_rate; 
} 
// Here regular_pay might not have been calculated 
total_pay = overtime_hours * overtime_rate * pay_rate + regular_pay; 

當你真正想要的可能是這樣的:

regular_pay = 40 * pay_rate; 
total_pay = regular_pay; 
if (overtime_hours > 0) { 
    total_pay = total_pay + overtime_hours * overtime_rate * pay_rate; 
}  
+0

這是OAMPL還是原始碼? – GitaarLAB

+0

@GitaarLAB:這是C語言,我用它作爲「僞」代碼來指出OP代碼中的邏輯缺陷,並提出更合適的代碼流。我將把它留給OP來完成實際的彙編代碼更改。 – Michael