九、if-else 逆向代码

 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3 004113B0  push        ebp  
 4 004113B1  mov         ebp,esp 
 5 004113B3  sub         esp,0CCh 
 6 004113B9  push        ebx  
 7 004113BA  push        esi  
 8 004113BB  push        edi  
 9 004113BC  lea         edi,[ebp-0CCh] 
10 004113C2  mov         ecx,33h 
11 004113C7  mov         eax,0CCCCCCCCh 
12 004113CC  rep stos    dword ptr es:[edi] 
13     int c;
14     printf("input number\n");
15 004113CE  mov         esi,esp 
16 004113D0  push        offset string "input number\n" (415770h) 
17 004113D5  call        dword ptr [__imp__printf (4182BCh)] 
18 004113DB  add         esp,4 
19 004113DE  cmp         esi,esp 
20 004113E0  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
21     scanf("%d",&c);
22 004113E5  mov         esi,esp 
23 004113E7  lea         eax,[c] 
24 004113EA  push        eax  
25 004113EB  push        offset string "%d" (41576Ch) 
26 004113F0  call        dword ptr [__imp__scanf (4182C4h)] 
27 004113F6  add         esp,8 
28 004113F9  cmp         esi,esp 
29 004113FB  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
30     if( (c>0) && (c<10) )
31 00411400  cmp         dword ptr [c],0 
32 00411404  jle         wmain+75h (411425h) 
33 00411406  cmp         dword ptr [c],0Ah 
34 0041140A  jge         wmain+75h (411425h) 
35     {
36         printf("a>0 && a<10");
37 0041140C  mov         esi,esp 
38 0041140E  push        offset string "a>0 && a<10" (41575Ch) 
39 00411413  call        dword ptr [__imp__printf (4182BCh)] 
40 00411419  add         esp,4 
41 0041141C  cmp         esi,esp 
42 0041141E  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
43 00411423  jmp         wmain+0B1h (411461h) 
44     }
45     else if (  (c>10) && (c<100) )
46 00411425  cmp         dword ptr [c],0Ah 
47 00411429  jle         wmain+9Ah (41144Ah) 
48 0041142B  cmp         dword ptr [c],64h 
49 0041142F  jge         wmain+9Ah (41144Ah) 
50     {
51         printf("c>10 && a<100");
52 00411431  mov         esi,esp 
53 00411433  push        offset string "c>10 && a<100" (41574Ch) 
54 00411438  call        dword ptr [__imp__printf (4182BCh)] 
55 0041143E  add         esp,4 
56 00411441  cmp         esi,esp 
57 00411443  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
58     }
59     else
60 00411448  jmp         wmain+0B1h (411461h) 
61     {
62         printf("a<0 || a>100");
63 0041144A  mov         esi,esp 
64 0041144C  push        offset string "a<0 || a>100" (41573Ch) 
65 00411451  call        dword ptr [__imp__printf (4182BCh)] 
66 00411457  add         esp,4 
67 0041145A  cmp         esi,esp 
68 0041145C  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
69     }
70     return 0;
71 00411461  xor         eax,eax 
72 }
73 00411463  push        edx  
74 00411464  mov         ecx,ebp 
75 00411466  push        eax  
76 00411467  lea         edx,[ (411488h)] 
77 0041146D  call        @ILT+135(@_RTC_CheckStackVars@8) (41108Ch) 
78 00411472  pop         eax  
79 00411473  pop         edx  
80 00411474  pop         edi  
81 00411475  pop         esi  
82 00411476  pop         ebx  
83 00411477  add         esp,0CCh 
84 0041147D  cmp         ebp,esp 
85 0041147F  call        @ILT+325(__RTC_CheckEsp) (41114Ah) 
86 00411484  mov         esp,ebp 
87 00411486  pop         ebp  
88 00411487  ret   

对应的c++代码:

 1 #include "stdafx.h"
 2 
 3 
 4 int _tmain(int argc, _TCHAR* argv[])
 5 {
 6     int c;
 7     printf("input number\n");
 8     scanf("%d",&c);
 9     if( (c>0) && (c<10) )
10     {
11         printf("a>0 && a<10");
12     }
13     else if (  (c>10) && (c<100) )
14     {
15         printf("c>10 && a<100");
16     }
17     else
18     {
19         printf("a<0 || a>100");
20     }
21     return 0;
22 }

对于if(a && b) 的情况,一般都是使用否决法,即如果A不成立,则立即跳转到下一个分支。同理,如果B不成立,也跳转到下一个分支。

1 cmp <条件>
2 jle <下一个分支>

.....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的逆向强化学习实例代码,使用了Python和TensorFlow库: ```python import numpy as np import tensorflow as tf # 定义环境模型,这里使用一个简单的网格世界 class GridWorld: def __init__(self): self.grid = np.zeros((4, 4)) self.grid[3, 3] = 1 # 目标位置 self.state = (0, 0) # 初始状态 def step(self, action): if action == 'up': self.state = (max(self.state[0] - 1, 0), self.state[1]) elif action == 'down': self.state = (min(self.state[0] + 1, 3), self.state[1]) elif action == 'left': self.state = (self.state[0], max(self.state[1] - 1, 0)) elif action == 'right': self.state = (self.state[0], min(self.state[1] + 1, 3)) if self.state == (3, 3): reward = 1 done = True else: reward = 0 done = False return self.state, reward, done # 定义逆向强化学习算法 class IRL: def __init__(self, expert_trajectories, num_states, num_actions, learning_rate=0.01): self.expert_trajectories = expert_trajectories self.num_states = num_states self.num_actions = num_actions self.learning_rate = learning_rate self.weights = tf.Variable(tf.random.normal((num_states, num_actions))) def compute_policy(self, state): logits = tf.matmul(state, self.weights) return tf.nn.softmax(logits) def train(self): optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate) for expert_trajectory in self.expert_trajectories: states, actions = expert_trajectory with tf.GradientTape() as tape: state_tensor = tf.convert_to_tensor(states, dtype=tf.float32) action_tensor = tf.one_hot(actions, depth=self.num_actions) policy = self.compute_policy(state_tensor) loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=action_tensor, logits=policy)) gradients = tape.gradient(loss, [self.weights]) optimizer.apply_gradients(zip(gradients, [self.weights])) # 创建一个网格世界环境实例 env = GridWorld() # 生成一些专家轨迹数据 expert_trajectories = [([env.step('right')[0] for _ in range(4)], ['right', 'right', 'right', 'down']), ([env.step('down')[0] for _ in range(4)], ['down', 'down', 'down', 'right'])] # 创建一个逆向强化学习模型实例 irl = IRL(expert_trajectories, num_states=16, num_actions=4) # 训练逆向强化学习模型 irl.train() ``` 这个例子展示了一个简单的网格世界环境,使用逆向强化学习算法从专家轨迹中学习一个策略。模型通过迭代优化权重来逼近专家的行为特征。请注意,这只是一个简单的示例,实际中可能需要更复杂的环境和更强大的模型来解决实际问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值